Create a gist now

Instantly share code, notes, and snippets.

@nikic /bench.php
Last active Mar 23, 2017

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
@Appla
Appla commented Mar 23, 2017

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

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