-
-
Save nikic/6390366 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"; | |
} |
// RUN 1: | |
cufa with 0 args took 0.43453288078308 | |
switch with 0 args took 0.24134302139282 | |
unpack with 0 args took 0.12418699264526 | |
cufa with 1 args took 0.52508997917175 | |
switch with 1 args took 0.27617788314819 | |
unpack with 1 args took 0.1316659450531 | |
cufa with 2 args took 0.5653350353241 | |
switch with 2 args took 0.37865686416626 | |
unpack with 2 args took 0.15234017372131 | |
cufa with 3 args took 0.61699819564819 | |
switch with 3 args took 0.39212608337402 | |
unpack with 3 args took 0.15739798545837 | |
cufa with 4 args took 0.66614294052124 | |
switch with 4 args took 0.44650483131409 | |
unpack with 4 args took 0.17123508453369 | |
cufa with 5 args took 0.73408579826355 | |
switch with 5 args took 0.49595499038696 | |
unpack with 5 args took 0.18640494346619 | |
cufa with 100 args took 5.0327250957489 | |
switch with 100 args took 5.291127204895 | |
unpack with 100 args took 1.2362589836121 | |
// RUN 2: | |
cufa with 0 args took 0.43715286254883 | |
switch with 0 args took 0.23759603500366 | |
unpack with 0 args took 0.12323498725891 | |
cufa with 1 args took 0.52805018424988 | |
switch with 1 args took 0.28081107139587 | |
unpack with 1 args took 0.14142084121704 | |
cufa with 2 args took 0.58326005935669 | |
switch with 2 args took 0.37855386734009 | |
unpack with 2 args took 0.14764094352722 | |
cufa with 3 args took 0.63784694671631 | |
switch with 3 args took 0.40486693382263 | |
unpack with 3 args took 0.16130805015564 | |
cufa with 4 args took 0.67221808433533 | |
switch with 4 args took 0.45406103134155 | |
unpack with 4 args took 0.16867184638977 | |
cufa with 5 args took 0.71207308769226 | |
switch with 5 args took 0.49642992019653 | |
unpack with 5 args took 0.18695878982544 | |
cufa with 100 args took 5.0643861293793 | |
switch with 100 args took 5.3053078651428 | |
unpack with 100 args took 1.1865899562836 | |
// RUN 3: | |
cufa with 0 args took 0.44004893302917 | |
switch with 0 args took 0.24612522125244 | |
unpack with 0 args took 0.1329391002655 | |
cufa with 1 args took 0.54023694992065 | |
switch with 1 args took 0.28621506690979 | |
unpack with 1 args took 0.133131980896 | |
cufa with 2 args took 0.59014391899109 | |
switch with 2 args took 0.38013100624084 | |
unpack with 2 args took 0.14762496948242 | |
cufa with 3 args took 0.62308812141418 | |
switch with 3 args took 0.39890003204346 | |
unpack with 3 args took 0.16549801826477 | |
cufa with 4 args took 0.65626192092896 | |
switch with 4 args took 0.45355796813965 | |
unpack with 4 args took 0.17756295204163 | |
cufa with 5 args took 0.72181010246277 | |
switch with 5 args took 0.51895904541016 | |
unpack with 5 args took 0.19365406036377 | |
cufa with 100 args took 5.0851991176605 | |
switch with 100 args took 5.5029859542847 | |
unpack with 100 args took 1.2169179916382 |
PHP 7.4.4 (cli) (built: Mar 20 2020 13:47:45) ( NTS )
cufa with 0 args took 0.088996887207031
switch with 0 args took 0.12057995796204
unpack with 0 args took 0.089555025100708
cufa with 1 args took 0.094866991043091
switch with 1 args took 0.16432213783264
unpack with 1 args took 0.096856117248535
cufa with 2 args took 0.099807977676392
switch with 2 args took 0.18916296958923
unpack with 2 args took 0.1002779006958
cufa with 3 args took 0.096481084823608
switch with 3 args took 0.22327995300293
unpack with 3 args took 0.10423707962036
cufa with 4 args took 0.099453210830688
switch with 4 args took 0.23742890357971
unpack with 4 args took 0.10180187225342
cufa with 5 args took 0.10233592987061
switch with 5 args took 0.26353788375854
unpack with 5 args took 0.10264611244202
cufa with 100 args took 0.29465198516846
switch with 100 args took 0.33954095840454
unpack with 100 args took 0.30862307548523
PHP 8.2.9 (cli) (built: Aug 16 2023 21:06:19) (NTS)
cufa with 0 args took 0.13467407226562
switch with 0 args took 0.16155314445496
unpack with 0 args took 0.13420295715332
cufa with 1 args took 0.14841604232788
switch with 1 args took 0.17904305458069
unpack with 1 args took 0.14671421051025
cufa with 2 args took 0.15329194068909
switch with 2 args took 0.19096398353577
unpack with 2 args took 0.15721797943115
cufa with 3 args took 0.15763902664185
switch with 3 args took 0.20245814323425
unpack with 3 args took 0.15891194343567
cufa with 4 args took 0.1690821647644
switch with 4 args took 0.21324777603149
unpack with 4 args took 0.16468191146851
cufa with 5 args took 0.16860508918762
switch with 5 args took 0.22226405143738
unpack with 5 args took 0.16858100891113
cufa with 100 args took 0.69865202903748
switch with 100 args took 0.73545598983765
unpack with 100 args took 0.68205690383911
PHP 7.3.11 (cli) (built: Oct 22 2019 08:11:04) ( NTS )
cufa with 0 args took 0.08603310585022
switch with 0 args took 0.091425180435181
unpack with 0 args took 0.071292877197266
cufa with 1 args took 0.077404975891113
switch with 1 args took 0.086993932723999
unpack with 1 args took 0.07887601852417
cufa with 2 args took 0.076728105545044
switch with 2 args took 0.10414099693298
unpack with 2 args took 0.087504148483276
cufa with 3 args took 0.08784294128418
switch with 3 args took 0.12477707862854
unpack with 3 args took 0.089874982833862
cufa with 4 args took 0.091574907302856
switch with 4 args took 0.13632702827454
unpack with 4 args took 0.10570001602173
cufa with 5 args took 0.091580152511597
switch with 5 args took 0.18238997459412
unpack with 5 args took 0.082036972045898
cufa with 100 args took 0.50029802322388
switch with 100 args took 0.54648208618164
unpack with 100 args took 0.60970711708069