Created
August 10, 2016 13:06
-
-
Save nkcmr/4e7497f4c9f70b94ca9e36dceb5204d4 to your computer and use it in GitHub Desktop.
poor man's php benchmark
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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