Skip to content

Instantly share code, notes, and snippets.

@zvoanhkietz
Created June 30, 2017 06:02
Show Gist options
  • Save zvoanhkietz/2315ba100a0ceca3adc5939603c21e9f to your computer and use it in GitHub Desktop.
Save zvoanhkietz/2315ba100a0ceca3adc5939603c21e9f to your computer and use it in GitHub Desktop.
<?php error_reporting(E_ALL);
function test() {}
$nIter = 1000000;
$argNums = [0, 1, 2, 3, 4, 5, 100];
$func = 'test';
foreach ($argNums as $argNum) {
$args = $argNum == 0 ? [] : array_fill(0, $argNum, null);
$startTime = microtime(true);
for ($i = 0; $i < $nIter; ++$i) {
call_user_func_array($func, $args);
}
$endTime = microtime(true);
echo "cufa with $argNum args took ", $endTime - $startTime, "\n";
$startTime = microtime(true);
for ($i = 0; $i < $nIter; ++$i) {
switch (count($args)) {
case 0: $func(); break;
case 1: $func($args[0]); break;
case 2: $func($args[0], $args[1]); break;
case 3: $func($args[0], $args[1], $args[2]); break;
case 4: $func($args[0], $args[1], $args[2], $args[3]); break;
case 5: $func($args[0], $args[1], $args[2], $args[3], $args[4]); break;
default: call_user_func_array($func, $args); break;
}
}
$endTime = microtime(true);
echo "switch with $argNum args took ", $endTime - $startTime, "\n";
$startTime = microtime(true);
for ($i = 0; $i < $nIter; ++$i) {
$func(...$args);
}
$endTime = microtime(true);
echo "unpack with $argNum args took ", $endTime - $startTime, "\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment