-
-
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.2.7-1+ubuntu18.04.1+deb.sury.org+1 (cli) (built: Jun 22 2018 08:45:49) ( NTS )
cufa with 0 args took 0.39157581329346
switch with 0 args took 0.49720907211304
unpack with 0 args took 0.42411303520203
cufa with 1 args took 0.40112709999084
switch with 1 args took 0.51593208312988
unpack with 1 args took 0.4350368976593
cufa with 2 args took 0.40166711807251
switch with 2 args took 0.55450797080994
unpack with 2 args took 0.43915009498596
cufa with 3 args took 0.40222883224487
switch with 3 args took 0.56734490394592
unpack with 3 args took 0.4390389919281
cufa with 4 args took 0.41575217247009
switch with 4 args took 0.58732008934021
unpack with 4 args took 0.44336700439453
cufa with 5 args took 0.41258883476257
switch with 5 args took 0.61525797843933
unpack with 5 args took 0.4450101852417
cufa with 100 args took 0.64909887313843
switch with 100 args took 0.74633812904358
unpack with 100 args took 0.70303606987
PHP 7.2.7 (cli) (built: Jun 19 2018 23:13:48) ( NTS MSVC15 (Visual C++ 2017) x64 )
cufa with 0 args took 0.064092874526978
switch with 0 args took 0.065840005874634
unpack with 0 args took 0.048593997955322
cufa with 1 args took 0.068807125091553
switch with 1 args took 0.085685014724731
unpack with 1 args took 0.05579686164856
cufa with 2 args took 0.079151153564453
switch with 2 args took 0.11850190162659
unpack with 2 args took 0.063709020614624
cufa with 3 args took 0.079231023788452
switch with 3 args took 0.13227415084839
unpack with 3 args took 0.065793991088867
cufa with 4 args took 0.10291719436646
switch with 4 args took 0.17569708824158
unpack with 4 args took 0.083001136779785
cufa with 5 args took 0.085749864578247
switch with 5 args took 0.16400909423828
unpack with 5 args took 0.073173046112061
cufa with 100 args took 0.35412693023682
switch with 100 args took 0.38690090179443
unpack with 100 args took 0.36837601661682
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
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
5.6.30
cufa with 0 args took 0.31852102279663
switch with 0 args took 0.18962597846985
unpack with 0 args took 0.10228705406189
cufa with 1 args took 0.37692308425903
switch with 1 args took 0.22077298164368
unpack with 1 args took 0.11410093307495
cufa with 2 args took 0.42086696624756
switch with 2 args took 0.25744605064392
unpack with 2 args took 0.12687015533447
cufa with 3 args took 0.45022988319397
switch with 3 args took 0.28524899482727
unpack with 3 args took 0.13288807868958
cufa with 4 args took 0.48601078987122
switch with 4 args took 0.3178379535675
unpack with 4 args took 0.14534306526184
cufa with 5 args took 0.52559208869934
switch with 5 args took 0.34833693504333
unpack with 5 args took 0.15302515029907
cufa with 100 args took 3.4251019954681
switch with 100 args took 3.6294660568237
unpack with 100 args took 1.101496219635
7.0.17
cufa with 0 args took 0.081735849380493
switch with 0 args took 0.10955095291138
unpack with 0 args took 0.072259902954102
cufa with 1 args took 0.089179039001465
switch with 1 args took 0.14180207252502
unpack with 1 args took 0.077605009078979
cufa with 2 args took 0.091766834259033
switch with 2 args took 0.16182899475098
unpack with 2 args took 0.081001996994019
cufa with 3 args took 0.096024036407471
switch with 3 args took 0.20374298095703
unpack with 3 args took 0.087507009506226
cufa with 4 args took 0.10052800178528
switch with 4 args took 0.22109603881836
unpack with 4 args took 0.090549945831299
cufa with 5 args took 0.10586500167847
switch with 5 args took 0.23921203613281
unpack with 5 args took 0.098327159881592
cufa with 100 args took 0.39941906929016
switch with 100 args took 0.4991409778595
unpack with 100 args took 0.419842004776
7.1.3
cufa with 0 args took 0.074923992156982
switch with 0 args took 0.11069107055664
unpack with 0 args took 0.07165002822876
cufa with 1 args took 0.080728054046631
switch with 1 args took 0.14000511169434
unpack with 1 args took 0.076350927352905
cufa with 2 args took 0.084750175476074
switch with 2 args took 0.16004991531372
unpack with 2 args took 0.081204891204834
cufa with 3 args took 0.089881181716919
switch with 3 args took 0.20446705818176
unpack with 3 args took 0.086418867111206
cufa with 4 args took 0.091805934906006
switch with 4 args took 0.21480107307434
unpack with 4 args took 0.088521003723145
cufa with 5 args took 0.095852851867676
switch with 5 args took 0.24212884902954
unpack with 5 args took 0.091315984725952
cufa with 100 args took 0.37423300743103
switch with 100 args took 0.48270487785339
unpack with 100 args took 0.41387891769409