Created
February 28, 2012 17:09
-
-
Save daGrevis/1933740 to your computer and use it in GitHub Desktop.
Benchmark boilerplate (in PHP)
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 | |
| 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, | |
| )); |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output, for me, looks like: