Skip to content

Instantly share code, notes, and snippets.

@blongden
Created April 10, 2012 16:22
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save blongden/2352583 to your computer and use it in GitHub Desktop.
Save blongden/2352583 to your computer and use it in GitHub Desktop.
Benchmark a PHP function
<?php
$calibration = benchmark(function() { });
$benchmark = benchmark(function() {
sleep(1);
});
echo "Calibration run: ".number_format($calibration)."/sec\n";
echo "Benchmark run: ".number_format($benchmark)."/sec\n";
echo 'Approximate code execution time (seconds): '.number_format((1/$benchmark) - (1/$calibration), 10);
function benchmark($x)
{
$start = $t = microtime(true);
$total = $c = $loop = 0;
while (true) {
$x();
$c++;
$now = microtime(true);
if ($now - $t > 1) {
$loop++;
$total += $c;
list($t, $c) = array(microtime(true), 0);
}
if ($now - $start > 2) {
return round($total / $loop);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment