Created
April 4, 2016 13:26
-
-
Save te2u/4e055e963e1ce04c13406b8b05f6933e to your computer and use it in GitHub Desktop.
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 | |
runs(array(1, 1, 10, 100, 1000, 10000, 100000, 1000000), array( | |
'$count' => function () { | |
$data = range(1, 25); | |
for ($i = 0, $count = count($data); $i < $count; $i++) { | |
$data[$i]; | |
} | |
}, | |
'count()' => function () { | |
$data = range(1, 25); | |
for ($i = 0; $i < count($data); $i++) { | |
$data[$i]; | |
} | |
}, | |
)); | |
function runs($counts, $functions, $interbal=null) | |
{ | |
foreach ($counts as $count) { | |
echo "$count\n"; | |
run($count, $functions, $interbal); | |
} | |
} | |
// See Also: http://d.hatena.ne.jp/do_aki/20100202/1265126448 | |
function run($cnt, $functions, $interbal=null) | |
{ | |
$times = array(); | |
foreach ($functions as $name => $func) { | |
$t = microtime(true); | |
for($i=0;$i<$cnt;++$i) { $func(); } | |
$t = microtime(true) - $t; | |
$times[$name] = $t; | |
if ($interbal) { | |
$interbal(); | |
} | |
} | |
$min = min($times); | |
if (0 == $min) { | |
echo "failed estimate ...\n"; | |
return; | |
} | |
foreach($times as $test_name => $time){ | |
$par = ($time/$min)*100; | |
$unit = $time / $cnt; | |
printf("%-16s: %10s sec %10s sec (%0.2f%%)\n" | |
, $test_name | |
, sprintf("%-0.4f", $time) | |
, sprintf("@ %-0.4f", $unit) | |
, $par); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment