Skip to content

Instantly share code, notes, and snippets.

@te2u
Created April 4, 2016 13:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save te2u/4e055e963e1ce04c13406b8b05f6933e to your computer and use it in GitHub Desktop.
Save te2u/4e055e963e1ce04c13406b8b05f6933e to your computer and use it in GitHub Desktop.
<?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