Created
September 6, 2015 07:04
-
-
Save harikt/6fcac9827790a15c4378 to your computer and use it in GitHub Desktop.
"Benchmarking call_user_func_array()" http://paul-m-jones.com/archives/182 . Modified the file from https://web.archive.org/web/20090305231052/http://paul-m-jones.com/public/func_speed.phps
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
{ | |
"require": { | |
"hoa/bench":"~2.0" | |
} | |
} |
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 | |
require __DIR__ . '/vendor/autoload.php'; | |
class example { | |
function html($value) { | |
return htmlentities($value); | |
} | |
} | |
function html($value) { | |
return htmlentities($value); | |
} | |
$timer = new Hoa\Bench\Bench(); | |
$value = "<bold>this and that</bold>\n"; | |
$array = (array) $value; | |
$example = new example(); | |
$func = 'html'; | |
$k = 10000000; | |
$timer->native->start(); | |
// native php | |
for ($i = 0; $i < $k; $i++) { | |
$out = htmlentities($value); | |
} | |
$timer->native->stop(); | |
$timer->literal_func->start(); | |
// literal func | |
for ($i = 0; $i < $k; $i++) { | |
$out = html($value); | |
} | |
$timer->literal_func->stop(); | |
$timer->variable_func->start(); | |
// variable-function | |
for ($i = 0; $i < $k; $i++) { | |
$out = $func($value); | |
} | |
$timer->variable_func->stop(); | |
$timer->literal_method->start(); | |
// literal method | |
for ($i = 0; $i < $k; $i++) { | |
$out = $example->html($value); | |
} | |
$timer->literal_method->stop(); | |
$timer->variable_method->start(); | |
// varible-method | |
for ($i = 0; $i < $k; $i++) { | |
$out = $example->$func($value); | |
} | |
$timer->variable_method->stop(); | |
$timer->call_func->start(); | |
// call_user_func (func) | |
for ($i = 0; $i < $k; $i++) { | |
$out = call_user_func($func, $value); | |
} | |
$timer->call_func->stop(); | |
$timer->call_object->start(); | |
// call_user_func (object) | |
for ($i = 0; $i < $k; $i++) { | |
$out = call_user_func( | |
array($example, $func), | |
$value | |
); | |
} | |
$timer->call_object->stop(); | |
$timer->cufa_func->start(); | |
// call_user_func_array (func) | |
for ($i = 0; $i < $k; $i++) { | |
$out = call_user_func_array($func, $array); | |
} | |
$timer->cufa_func->stop(); | |
$timer->cufa_object->start(); | |
// call_user_func_array (object) | |
for ($i = 0; $i < $k; $i++) { | |
$out = call_user_func_array( | |
array($example, $func), | |
$array | |
); | |
} | |
$timer->cufa_object->stop(); | |
echo $timer; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Motivation : Looking at veonik/php-router-benchmark#10 I feel aura v2 is outperforming due to some of the uses of call_user_func methods inside https://github.com/auraphp/Aura.Router/blob/2.x/src/Router.php#L118 , https://github.com/auraphp/Aura.Router/blob/2.x/src/RouteCollection.php#L221 and 1000's of calls will actually be a trouble when benchmarking. In a real world we normally will never have 1000's of routes. I don't disagree that there will not be. But it can be a rare case if so.