How to find php execution time?

How to find php execution time?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

How to Find PHP Execution Time

Knowing exactly how long your application spends processing a request is critical for performance tuning, but the approach depends entirely on where you are measuring from. If you want to see the time spent strictly within your PHP script, using the microtime() function is the most direct method. This returns a float representing current timestamp in seconds with fractional seconds, which allows for precise measurement of code blocks.

To measure a specific section of your logic, record the start time before execution begins and subtract it from the end time recorded after the block completes. The standard pattern looks like this:

$startTime = microtime(true);

// Your large code here
// heavy database queries, complex loops, etc.

$endTime = microtime(true);
$executionTime = $endTime - $startTime;

echo "Script took {$executionTime} seconds to execute.";

Using microtime(true) is preferred over time() because the latter only returns an integer number of seconds since the Unix epoch. If your script runs in under a second, time() will report zero duration regardless of how many milliseconds were actually consumed. The fractional component provided by microtime(true) ensures you capture sub-second performance nuances.

However, measuring time inside the script only tells you about PHP's runtime. It does not include the overhead of parsing the request, initializing the bootstrap process, or executing any middleware that runs before your main logic starts. For a complete picture of the total response time, you must look at the web server logs. Apache and Nginx typically log the request_time directive, which represents the total duration from the first byte received to the last byte sent. This metric includes everything: HTTP headers, SSL handshakes, proxying time (if applicable), and the full PHP execution duration.

If you are using Laravel, there is a built-in feature that simplifies this significantly without writing custom timers. The framework provides Carbon for date manipulation and often integrates with tools to log performance metrics. Specifically, you can utilize the Request class or middleware to capture timing data at the entry point of your application. While Laravel doesn't have a single magic function to print total time instantly like a simple script, its event system allows you to hook into the request lifecycle. You could define a custom middleware that records the start timestamp in the $request->attributes array and another to calculate the difference before rendering the response.

// In your Application\Http\Middleware\HandleTimeMiddleware.php class
use Illuminate\Http\Request;
use function Illuminate\Support\Facades\Artisan;

public function handle(Request $request, \Closure $next)
{
    $startTime = microtime(true);
    
    // Handle the incoming request
    $response = $next($request);

    $executionTime = microtime(true) - $startTime;
    
    // Log to your preferred system or attach to response header
    if ($executionTime > 0.5) { // Example threshold for logging warnings
        \Log::warning("Slow request detected: " . $executionTime . 's');
    }

    return $response;
}

Recording execution time is often the first step, but understanding what contributes to that duration is equally important. A high number might indicate a slow database query, an inefficient loop, or excessive memory swapping. Tools like Blackfire or Laravel Debugbar can provide deep visibility into where those milliseconds are being spent, breaking down time by route, controller method, and even individual queries.

When analyzing results, remember that the environment matters. Execution times will vary based on server load, PHP version (8.0 vs 8.3 offers significant performance gains in certain areas), and opcode caching configurations like OPcache. Always benchmark in an environment that resembles production to avoid false positives. If your application is hosted by a provider like LaravelCompany, their infrastructure usually includes built-in monitoring dashboards that aggregate this data across multiple instances, giving you a high-level view of execution trends without needing to instrument every single line of code manually.

Our network