Skip to content

Instantly share code, notes, and snippets.

@alexkunin
Created June 7, 2018 14:32
Show Gist options
  • Save alexkunin/92c33cc20b491c91c36db003f4971c67 to your computer and use it in GitHub Desktop.
Save alexkunin/92c33cc20b491c91c36db003f4971c67 to your computer and use it in GitHub Desktop.
<?php
function func($param) {
return $param;
}
class A
{
public static function staticMethod($param) {
return $param;
}
}
class B
{
public function method($param) {
return $param;
}
}
$lambda = function ($param) {
return $param;
};
$dummy = 123;
$closure = function ($param) use ($dummy) {
return $param;
};
$n = 10000000;
$start = null;
function start($message) {
global $start;
printf("%-50s", $message);
$start = microtime(true);
}
function stop() {
global $start, $n;
$stop = microtime(true);
printf("%d ms\n", ($stop - $start) * 1000);
}
echo "FUNCTION\n\n";
start("func(...) ");
for ($i = 0; $i < $n; $i++) func(1);
stop();
start("\$func = 'func'; \$func(...)");
$func = 'func';
for ($i = 0; $i < $n; $i++) $func(1);
stop();
start("call_user_func('func', ...)");
$func = 'func';
for ($i = 0; $i < $n; $i++) call_user_func('func', 1);
stop();
echo "\nSTATIC METHOD\n\n";
start("A::staticMethod(...)");
for ($i = 0; $i < $n; $i++) func(1);
stop();
start("\$func = ['A', 'staticMethod']; \$func(...)");
$func = ['A', 'staticMethod'];
for ($i = 0; $i < $n; $i++) $func(1);
stop();
start("\$func = 'A::staticMethod'; \$func(...)");
$func = 'A::staticMethod';
for ($i = 0; $i < $n; $i++) $func(1);
stop();
start("call_user_func(['A', 'staticMethod'], ...)");
for ($i = 0; $i < $n; $i++) call_user_func(['A', 'staticMethod'], 1);
stop();
echo "\nMETHOD\n\n";
$b = new B;
start("\$b->method(...)");
for ($i = 0; $i < $n; $i++) $b->method(1);
stop();
start("\$func = ['\$b', 'method']; \$func(...)");
$func = [$b, 'method'];
for ($i = 0; $i < $n; $i++) $func(1);
stop();
start("\$func = 'method'; \$b->{\$func}(...)");
$func = 'method';
for ($i = 0; $i < $n; $i++) $b->{$func}(1);
stop();
start("call_user_func([\$b, 'method'], ...)");
for ($i = 0; $i < $n; $i++) call_user_func([$b, 'method'], 1);
stop();
echo "\nLAMBDA\n\n";
start("\$lambda(...) ");
for ($i = 0; $i < $n; $i++) $lambda(1);
stop();
start("call_user_func(\$lambda, ...) ");
for ($i = 0; $i < $n; $i++) call_user_func($lambda, 1);
stop();
echo "\nCLOSURE\n\n";
start("\$closure(...) ");
for ($i = 0; $i < $n; $i++) $closure(1);
stop();
start("call_user_func(\$closure, ...) ");
for ($i = 0; $i < $n; $i++) call_user_func($closure, 1);
stop();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment