Skip to content

Instantly share code, notes, and snippets.

@harikt
Created September 6, 2015 07:04
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 harikt/6fcac9827790a15c4378 to your computer and use it in GitHub Desktop.
Save harikt/6fcac9827790a15c4378 to your computer and use it in GitHub Desktop.
{
"require": {
"hoa/bench":"~2.0"
}
}
<?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;
@harikt
Copy link
Author

harikt commented Sep 6, 2015

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.

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