Simplest way to profile a PHP script

Simplest way to profile a PHP script

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

Stefan Bogdanescu

Founder & Senior Architect

Simplest way to profile a PHP script

Manual timing with microtime() is a common starting point when developers need to understand performance bottlenecks. The snippet you attempted demonstrates the basic concept: record a start timestamp, execute the target function, record an end timestamp, and calculate the difference. However, relying on this manual approach for full script profiling introduces significant friction. You have to instrument every single function call if you want granular data, which is error-prone and slows down development. Furthermore, floating-point arithmetic in PHP can occasionally yield negative results due to precision limits when subtracting two very close timestamps, or the logic might fail if the start time isn't captured with enough decimal places (e.g., using microtime(true) vs microtime()).

While wrapping specific functions works for isolated testing, it does not scale for profiling an entire application. You need a solution that captures the execution path and timing without requiring you to manually insert code at every breakpoint. The industry-standard approach for this is Xdebug with its profiling extension or, more specifically for modern workflows, sampling profilers that integrate directly into the command line or IDE.

The most straightforward way to get a complete dump of function calls and their durations without modifying your source code is to use xdebug_profile_enable combined with xdebug_start_output if you are on older setups, but the modern equivalent is simply enabling Xdebug's profiling mode and outputting the results to a file. However, the absolute easiest method that requires zero changes to your application logic is using the built-in XDEBUG_PROFILE environment variable or utilizing tools like Blackfire or Tidal (though these often require setup).

If you are working within a Laravel ecosystem, there is an even simpler path. You can enable Xdebug's profiling feature globally and have it generate a cachegrind.out.XXX file automatically. This file contains the full call graph and timing information for every function executed during that specific request. Once generated, you can open this .cachegrind file in KCacheGrind, a graphical viewer that renders the data into a readable flame graph showing exactly where time was spent.

For a quick command-line solution without external dependencies like Xdebug installed, you can utilize the xhprof extension if available in your PHP build. By setting XDEBUG_PROFILE=1, Xdebug will automatically generate a profile for the current request. This is particularly useful when debugging a specific script that is running slowly. You simply add <?php ini_set('xdebug.profiler_enable', 1); ?> at the top of the file, or better yet, configure your .env file to set XDEBUG_PROFILE=1. When the script runs, Xdebug writes the profile data to a designated directory (usually defined by xdebug.profiler_output_dir).

The trade-off here is disk I/O. Generating a full call graph for a high-traffic production server can fill up your disk space rapidly because the profiling data is verbose. Therefore, this technique should be reserved for staging environments or specific debugging sessions, not live production traffic. Additionally, enabling profiling will inevitably slow down the script itself, so it must be disabled after the debugging session is complete.

If you prefer a lighter weight approach that doesn't require Xdebug, you can use pcntl_signal to catch signals and dump stats, but this is more complex than necessary for simple function timing. The manual microtime() method remains useful only for quick sanity checks on specific logic blocks where the overhead of instrumentation tools feels too heavy. For a comprehensive view of all function calls and durations across your entire script, leveraging Xdebug's automatic profiling output is the most robust and least intrusive strategy available in the PHP ecosystem.

For those managing Laravel applications specifically, integrating this into your deployment pipeline ensures that performance regressions are caught early. You can automate the generation of these profiles by triggering a specific test suite with the profile enabled, then parsing the resulting .cachegrind files to identify slow endpoints automatically before merging code. This keeps the profiling data actionable and tied directly to your CI/CD workflow.

Our network