Benchmarks
suggest changeBenchmarking allows you to measure and compare the execution time and memory consumption of different code implementations in PHP. Reliable benchmarks require high-resolution timers, isolated test runs, and consideration of PHP's OPcache.
High-Precision Timing with hrtime()
PHP 7.3 introduced hrtime(), which returns high-resolution monotonic time. Unlike microtime(), hrtime() is not affected by system clock adjustments (such as NTP time sync):
// hrtime(true) returns nanoseconds as an integer
$start = hrtime(true);
// Code to measure
$result = [];
for ($i = 0; $i < 100_000; $i++) {
$result[] = $i * 2;
}
$end = hrtime(true);
$durationMs = ($end - $start) / 1_000_000;
echo sprintf("Execution time: %.4f ms\n", $durationMs);
echo sprintf("Memory used: %.2f KB\n", memory_get_usage() / 1024);
Writing a Micro-Benchmark Helper
When comparing two algorithms, always include warm-up iterations to prime OPcache and CPU caches, and take multiple iterations to average out system noise:
function benchmark(string $label, callable $fn, int $iterations = 10_000, int $warmup = 1_000): void
{
// Warm-up phase
for ($i = 0; $i < $warmup; $i++) {
$fn();
}
gc_collect_cycles(); // Clean up garbage before timing
$startMemory = memory_get_usage();
$startTime = hrtime(true);
for ($i = 0; $i < $iterations; $i++) {
$fn();
}
$endTime = hrtime(true);
$endMemory = memory_get_usage();
$totalMs = ($endTime - $startTime) / 1_000_000;
$avgPerOpUs = (($endTime - $startTime) / $iterations) / 1_000;
$memoryDeltaKb = ($endMemory - $startMemory) / 1024;
printf(
"[%s]\n Total: %.2f ms | Avg: %.4f μs/op | Mem: %.2f KB\n",
$label,
$totalMs,
$avgPerOpUs,
$memoryDeltaKb
);
}
// Example: Comparing `in_array` with array search vs `isset` hash lookup
$elements = range(1, 10_000);
$hash = array_flip($elements);
$needle = 9_999;
benchmark('in_array (strict)', function () use ($elements, $needle) {
return in_array($needle, $elements, true);
}, 10_000);
benchmark('isset with hash map', function () use ($hash, $needle) {
return isset($hash[$needle]);
}, 10_000);
Sample output (timings vary by machine):
[in_array (strict)]
Total: 84.12 ms | Avg: 8.4120 μs/op | Mem: 0.00 KB
[isset with hash map]
Total: 0.15 ms | Avg: 0.0150 μs/op | Mem: 0.00 KB
Professional Benchmarking with PHPBench
For large-scale projects, PHPBench is the industry-standard benchmarking framework, providing statistical analysis, warm-ups, and Markdown/HTML report generation.
Installation
composer require --dev phpbench/phpbench
Defining a Benchmark Class
Create a benchmark file, e.g. benchmarks/StringBenchmark.php:
namespace App\Tests\Benchmark;
/**
* @Warmup(2)
* @Revs(1000)
* @Iterations(5)
*/
class StringBenchmark
{
public function benchStringConcat(): void
{
$str = '';
for ($i = 0; $i < 100; $i++) {
$str .= 'sample_item_' . $i;
}
}
public function benchArrayImplode(): void
{
$arr = [];
for ($i = 0; $i < 100; $i++) {
$arr[] = 'sample_item_' . $i;
}
$str = implode('', $arr);
}
}
Running the Benchmark
./vendor/bin/phpbench run benchmarks/StringBenchmark.php --report=default
PHPBench runs each method through the specified iterations and revisions, outputting the average time per revolution for each iteration along with its deviation from the mean (use --report=aggregate for a per-subject summary with mode and relative standard deviation).
Benchmarking Guidelines
- Disable Xdebug: Never benchmark with
xdebugextension loaded, as instrumentation significantly distorts execution times. - Enable OPcache: Ensure
opcache.enable_cli=1inphp.iniso benchmarked CLI runs match production web execution conditions. - Isolate Environment: Run benchmarks when the machine is under minimal background load to prevent CPU throttling and scheduling anomalies.