-
-
Save nikic/85e4db8d96f38bc0383740583a7472f6 to your computer and use it in GitHub Desktop.
This file contains 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 error_reporting(E_ALL); | |
function formatT($t) { | |
return sprintf('%.1f mus', $t * 1000000); | |
} | |
function formatResult($result) { | |
list($mean, $stddev) = $result; | |
return formatT($mean) . " +/- " . formatT($stddev); | |
} | |
function median($dts) { | |
$numRuns = count($dts); | |
sort($dts); | |
if ($numRuns % 2 == 0) { | |
return $dts[$numRuns / 2]; | |
} else { | |
return ($dts[($numRuns - 1) / 2] + $dts[($numRuns + 1) / 2]) / 2; | |
} | |
} | |
function stddev($dts, $mean) { | |
$sum = 0; | |
foreach ($dts as $dt) { | |
$sum += ($dt - $mean) * ($dt - $mean); | |
} | |
return sqrt($sum / (count($dts) - 1)); | |
} | |
function nameWidth($names) { | |
$lens = array_map('strlen', $names); | |
$maxLen = max($lens); | |
return $maxLen; | |
} | |
function runTest(callable $test) { | |
$targetRuntime = 0.3; | |
$maxFactor = 10; | |
$numRuns = 6; | |
// Find appropriate $n to run for $targetTime | |
$n = 50; | |
while (true) { | |
$t = microtime(true); | |
$test($n); | |
$dt = microtime(true) - $t; | |
if ($dt >= $targetRuntime * 0.9) { | |
break; | |
} | |
$newN = $n * $targetRuntime / $dt; | |
$n = $newN < $maxFactor * $n ? $newN : $maxFactor * $n; | |
} | |
$dts = []; | |
for ($i = 0; $i < $numRuns; ++$i) { | |
$t = microtime(true); | |
$test($n); | |
$dts[] = microtime(true) - $t; | |
} | |
$mean = array_sum($dts) / count($dts); | |
$stddev = stddev($dts, $mean); | |
$mean /= $n; | |
$stddev /= $n; | |
//$median = median($dts); | |
//$median /= $n; | |
return [$mean, $stddev]; | |
} | |
function runTests(array $tests) { | |
$opts = getopt('', ['save:', 'against:']); | |
$baseline = null; | |
if (isset($opts['against'])) { | |
$file = $opts['against']; | |
if (!file_exists($file)) { | |
die("File $file does not exist\n"); | |
} | |
$baseline = json_decode(file_get_contents($file), true); | |
} | |
$nameWidth = nameWidth(array_keys($tests)); | |
$results = []; | |
foreach ($tests as $name => $test) { | |
echo str_pad($name . ": ", $nameWidth + 2); | |
$results[$name] = runTest($test); | |
echo formatResult($results[$name]); | |
if (null !== $baseline) { | |
echo " ", formatResult($baseline[$name]); | |
$mean = $results[$name][0]; | |
$baseMean = $baseline[$name][0]; | |
$change = ($mean - $baseMean) / $baseMean * 100; | |
echo " ", sprintf("%+6.1f", $change), "%"; | |
} | |
echo "\n"; | |
} | |
if (isset($opts['save'])) { | |
file_put_contents($opts['save'], json_encode($results)); | |
} | |
} | |
$tests = []; | |
$tests['Fixed string lookup'] = function($n) { | |
for ($i = 0; $i < $n; ++$i) { | |
$a = ["foo" => "bar"]; | |
for ($j = 0; $j < 1000; ++$j) { | |
$a["foo"]; | |
} | |
} | |
}; | |
$tests['Packed int insert'] = function($n) { | |
for ($i = 0; $i < $n; ++$i) { | |
$a = []; | |
for ($j = 0; $j < 1000; ++$j) { | |
$a[$j] = 0; | |
} | |
unset($a); | |
} | |
}; | |
$tests['Reverse int insert'] = function($n) { | |
for ($i = 0; $i < $n; ++$i) { | |
$a = []; | |
for ($j = 1000; $j >= 0; --$j) { | |
$a[$j] = 0; | |
} | |
unset($a); | |
} | |
}; | |
$tests['Reverse int insert x16 collisions'] = function($n) { | |
for ($i = 0; $i < $n; ++$i) { | |
$a = []; | |
for ($j = 16000; $j >= 0; $j -= 16) { | |
$a[$j] = 0; | |
} | |
unset($a); | |
} | |
}; | |
$tests['Incrementing string insert'] = function($n) { | |
for ($i = 0; $i < $n; ++$i) { | |
$a = []; | |
for ($j = 'a', $k = 0; $k < 1000; ++$j, ++$k) { | |
$a[$j] = 0; | |
} | |
unset($a); | |
} | |
}; | |
$tests['Packed int lookup'] = function($n) { | |
$a = []; | |
for ($j = 0; $j < 1000; ++$j) { | |
$a[$j] = 0; | |
} | |
for ($i = 0; $i < $n; ++$i) { | |
for ($j = 0; $j < 1000; ++$j) { | |
$a[$j]; | |
} | |
} | |
}; | |
$tests['Reverse int lookup'] = function($n) { | |
$a = []; | |
for ($j = 1000; $j >= 0; --$j) { | |
$a[$j] = 0; | |
} | |
for ($i = 0; $i < $n; ++$i) { | |
for ($j = 1000; $j >= 0; --$j) { | |
$a[$j]; | |
} | |
} | |
}; | |
$tests['Incrementing string lookup'] = function($n) { | |
$a = []; | |
for ($j = 'a', $k = 0; $k < 1000; ++$j, ++$k) { | |
$a[$j] = 0; | |
} | |
for ($i = 0; $i < $n; ++$i) { | |
for ($j = 'a', $k = 0; $k < 1000; ++$j, ++$k) { | |
$a[$j]; | |
} | |
} | |
}; | |
$tests['Longer string lookup'] = function($n) { | |
$a = []; | |
$j = 'aaaaaaaa'; | |
for ($k = 0; $k < 1000; ++$j, ++$k) { | |
$a[$j] = 0; | |
} | |
for ($i = 0; $i < $n; ++$i) { | |
$j = 'aaaaaaaa'; | |
for ($k = 0; $k < 1000; ++$j, ++$k) { | |
$a[$j]; | |
} | |
} | |
}; | |
$t = microtime(true); | |
runTests($tests); | |
var_dump(microtime(true) - $t); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment