Skip to content

Instantly share code, notes, and snippets.

@daGrevis
Created February 28, 2012 17:09
Show Gist options
  • Select an option

  • Save daGrevis/1933740 to your computer and use it in GitHub Desktop.

Select an option

Save daGrevis/1933740 to your computer and use it in GitHub Desktop.
Benchmark boilerplate (in PHP)
<?php
const TIMES = 1000000;
function bench($function, $times = null) {
$times = $times !== null ? $times : TIMES;
$begin = microtime(true);
for ($i = 0; $i < $times; ++$i) {
$function();
}
return microtime(true) - $begin;
}
function format_results(array $benchs) {
asort($benchs);
$output = '';
foreach ($benchs as $name => $time) {
$output .= sprintf('%s : %f<br>', $name, $time);
}
return $output;
}
/**
* BENCH ALL THE THINGS!
*/
$b1 = bench(function() {
foreach (range('a', 'z') as $el) {
// Pass.
}
});
$b2 = bench(function() {
$t = range('a', 'z');
foreach ($t as $el) {
// Pass.
}
});
echo format_results(array(
'with range() in foreach ()' => $b1,
'with range() outside of foreach ()' => $b2,
));
@daGrevis

Copy link
Copy Markdown
Author

Output, for me, looks like:

with range() in foreach () : 5.502829
with range() outside of foreach () : 5.618382

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment