Skip to content

Instantly share code, notes, and snippets.

@nkcmr
Created August 10, 2016 13:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nkcmr/4e7497f4c9f70b94ca9e36dceb5204d4 to your computer and use it in GitHub Desktop.
Save nkcmr/4e7497f4c9f70b94ca9e36dceb5204d4 to your computer and use it in GitHub Desktop.
poor man's php benchmark
<?php
define("BENCH_ITERATIONS", 1000);
$scenarios = [];
// scenario one
$scenarios['one'] = function () {
$start = microtime(true);
// do stuff in between the microtimes
return microtime(true) - $start;
};
// scenario two
$scenarios['two'] = function () {
$start = microtime(true);
return microtime(true) - $start;
};
foreach ($scenarios as $name => $fn) {
$times = [];
for ($i = 0; $i < BENCH_ITERATIONS; $i++) {
$times[] = call_user_func($fn);
}
fwrite(STDOUT, sprintf("scenario: %s -- average running time: %.3fs\n", $name, (array_sum($times) / BENCH_ITERATIONS)));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment