Skip to content

Instantly share code, notes, and snippets.

@jmwebservices
Forked from nikic/bench.php
Created August 24, 2018 09:56
Show Gist options
  • Save jmwebservices/804a39a88cf0eebd50911eaf52dd1a65 to your computer and use it in GitHub Desktop.
Save jmwebservices/804a39a88cf0eebd50911eaf52dd1a65 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment