Skip to content

Instantly share code, notes, and snippets.

@nkcmr

nkcmr/bmrk.php

Created Aug 10, 2016
Embed
What would you like to do?
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