Skip to content

Instantly share code, notes, and snippets.

@nikic
Last active November 2, 2023 22:49
Show Gist options
  • Save nikic/6390366 to your computer and use it in GitHub Desktop.
Save nikic/6390366 to your computer and use it in GitHub Desktop.
Benchmark of call_user_func_array vs switch optimization vs argument unpacking syntax
<?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
@slava-basko
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment