Skip to content

Instantly share code, notes, and snippets.

@barryvdh
Forked from nikic/bench.php
Last active February 2, 2016 08:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save barryvdh/c88c87b41afd0d88d13b to your computer and use it in GitHub Desktop.
Save barryvdh/c88c87b41afd0d88d13b 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) {
if (PHP_VERSION_ID >= 50600) {
$func(...$args);
}
}
$endTime = microtime(true);
echo "unpack with $argNum args took ", $endTime - $startTime, "\n";
}
// RUN 1:
cufa with 0 args took 0.51093697547913
switch with 0 args took 0.25961208343506
unpack with 0 args took 0.16498208045959
cufa with 1 args took 0.58492302894592
switch with 1 args took 0.32328605651855
unpack with 1 args took 0.17757987976074
cufa with 2 args took 0.62471890449524
switch with 2 args took 0.38140821456909
unpack with 2 args took 0.18650889396667
cufa with 3 args took 0.67326211929321
switch with 3 args took 0.42645883560181
unpack with 3 args took 0.19748115539551
cufa with 4 args took 0.71092510223389
switch with 4 args took 0.46344208717346
unpack with 4 args took 0.20987296104431
cufa with 5 args took 0.75272703170776
switch with 5 args took 0.50403499603271
unpack with 5 args took 0.21983790397644
cufa with 100 args took 4.6691329479218
switch with 100 args took 5.0100290775299
unpack with 100 args took 1.3475320339203
// RUN 2:
cufa with 0 args took 0.51002717018127
switch with 0 args took 0.25696516036987
unpack with 0 args took 0.16168093681335
cufa with 1 args took 0.58382201194763
switch with 1 args took 0.32041001319885
unpack with 1 args took 0.17517805099487
cufa with 2 args took 0.62337493896484
switch with 2 args took 0.38088703155518
unpack with 2 args took 0.18605399131775
cufa with 3 args took 0.67131018638611
switch with 3 args took 0.42596197128296
unpack with 3 args took 0.19695591926575
cufa with 4 args took 0.71094298362732
switch with 4 args took 0.46291899681091
unpack with 4 args took 0.20937609672546
cufa with 5 args took 0.75075888633728
switch with 5 args took 0.50380516052246
unpack with 5 args took 0.2197151184082
cufa with 100 args took 4.6559550762177
switch with 100 args took 5.0126330852509
unpack with 100 args took 1.3509678840637
// RUN 3:
cufa with 0 args took 0.5332179069519
switch with 0 args took 0.28537201881409
unpack with 0 args took 0.18562912940979
cufa with 1 args took 0.60794401168823
switch with 1 args took 0.34805607795715
unpack with 1 args took 0.19937896728516
cufa with 2 args took 0.6473650932312
switch with 2 args took 0.40976810455322
unpack with 2 args took 0.21083498001099
cufa with 3 args took 0.6942150592804
switch with 3 args took 0.45553708076477
unpack with 3 args took 0.22113919258118
cufa with 4 args took 0.73241019248962
switch with 4 args took 0.49041199684143
unpack with 4 args took 0.23208904266357
cufa with 5 args took 0.7748589515686
switch with 5 args took 0.53174185752869
unpack with 5 args took 0.24371910095215
cufa with 100 args took 4.7278261184692
switch with 100 args took 5.0705368518829
unpack with 100 args took 1.3685908317566
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment