Created
September 4, 2012 00:21
-
-
Save lavoiesl/3615243 to your computer and use it in GitHub Desktop.
Benchmark of different function calls in PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Commons | |
$callable = function() {}; | |
function empty_function() {}; | |
class A { | |
public static function empty_function() {} | |
public function empty_method () {} | |
} | |
$instance = new A; | |
// Tests | |
$tests['control'] = $callable; | |
$tests['cufa_fga'] = function() use ($callable) { | |
call_user_func_array($callable, func_get_args()); | |
}; | |
$tests['cufa_array'] = function() use ($callable) { | |
call_user_func_array($callable, array()); | |
}; | |
$tests['cuf'] = function() use ($callable) { | |
call_user_func($callable); | |
}; | |
$tests['variable'] = function() use ($callable) { | |
$callable(); | |
}; | |
$tests['static'] = function() use ($callable) { | |
A::empty_function(); | |
}; | |
$tests['method'] = function() use ($instance) { // Here we use $instance instead of $callable | |
$instance->empty_method(); | |
}; | |
$tests['direct'] = function() use ($callable) { | |
empty_function(); | |
}; | |
// Run tests | |
$n = 500000; | |
$results = array(); | |
foreach ($tests as $name => $func) { | |
$start = microtime(true); | |
for ($i=0; $i < $n; $i++) { | |
$func(); | |
} | |
$results[$name] = microtime(true) - $start; | |
} | |
// Output results, padded because it's beautiful | |
$control = $results['control']; | |
$lname = max(array_map('strlen', array_keys($results))); | |
$ltime = ceil(log(max($results) * 1000, 10)); | |
unset($results['control']); | |
asort($results); | |
printf("%{$lname}s: %{$ltime}dms\n", 'control', $control * 1000); | |
foreach ($results as $name => $time) { | |
printf( | |
"%{$lname}s: %{$ltime}dms (+ %{$ltime}dms)\n", | |
$name, | |
$time * 1000, | |
($time - $control) * 1000 | |
); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PHP 5.4.5 with Xdebug 2.2.1 and APC 3.1.10 | |
control: 1786ms | |
direct: 2682ms (+ 895ms) | |
static: 2726ms (+ 939ms) | |
method: 2766ms (+ 979ms) | |
variable: 3883ms (+ 2096ms) | |
cuf: 4862ms (+ 3075ms) | |
cufa_array: 5119ms (+ 3332ms) | |
cufa_fga: 5762ms (+ 3975ms) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Roule tes tests n times, prend la moyenne et la stddev et fait des intervalles de confiance :)