2013. május 7., kedd

PHP függvény hívás gyors teszt

in medias res:

<?php
$foo = function($a, $b, $c)
{
    $j = 5-10-34-1+3/5;
    //    echo 'foo function';
};


function foo($a, $b, $c)
{
    $j = 5-10-34-1+3/5;
}


class Bar
{
    public static function static_foo($a, $b, $c)
    {
        $j = 5-10-34-1+3/5;
    }
   
    public function foo($a, $b, $c)
    {
        $j = 5-10-34-1+3/5;
    }
   
   
}


$max = 10000;

echo '1 - '.$max;
echo '<br />';echo '<br />';


echo 'foo(1, 2, 3)';echo '<br />';
$i = 0;
$time = microtime(true);
while ($i<$max)
{
    foo(1, 2, 3);
    $i++;
}
echo microtime(true) - $time;
echo '<br />';echo '<br />';


echo '$foo = function($a, $b, $c){ /* ... */ }';echo '<br />';
echo '$foo(1,2,3)';echo '<br />';
$i = 0;
$time = microtime(true);
while ($i<$max)
{
    $foo(1, 2, 3);
    $i++;
}
echo microtime(true) - $time;
echo '<br />';echo '<br />';


echo '$function = "foo";';echo '<br />';
echo '$function(1, 2, 3)';echo '<br />';
$i = 0;
$function = 'foo';
$time = microtime(true);
while ($i<$max)
{
    $function(1, 2, 3);
    $i++;
}
echo microtime(true) - $time;
echo '<br />';echo '<br />';


echo "call_user_func('foo', 1, 2, 3)";echo '<br />';
$i = 0;
$time = microtime(true);
while ($i<$max)
{
    call_user_func('foo', 1, 2, 3);
    $i++;
}
echo microtime(true) - $time;
echo '<br />';echo '<br />';


echo "call_user_func_array('foo', array(1, 2, 3))";echo '<br />';
$i = 0;
$time = microtime(true);
while ($i<$max)
{
    call_user_func_array('foo', array(1, 2, 3));
    $i++;
}
echo microtime(true) - $time;
echo '<br />';echo '<br />';


echo "call_user_func(array('Bar', 'foo'), 1,2,3)";echo '<br />';
$i = 0;
$time = microtime(true);
while ($i<$max)
{
    call_user_func_array(array('Bar', 'foo'), array(1,2,3));
    $i++;
}
echo microtime(true) - $time;
echo '<br />';echo '<br />';


echo 'call_user_func_array(array("Bar", "foo"), array(1,2,3))';echo '<br />';
$i = 0;
$time = microtime(true);
while ($i<$max)
{
    call_user_func_array(array('Bar', 'foo'), array(1,2,3));
    $i++;
}
echo microtime(true) - $time;
echo '<br />';echo '<br />';


echo 'call_user_func_array("Bar::foo"), array(1,2,3))';echo '<br />';
$i = 0;
$time = microtime(true);
while ($i<$max)
{
    call_user_func_array('Bar::foo', array(1,2,3));
    $i++;
}
echo microtime(true) - $time;
echo '<br />';echo '<br />';


echo '$bar->{$function}(1, 2, 3)';echo '<br />';
$i = 0;
$bar = new Bar(1, 2, 3);
$time = microtime(true);
while ($i<$max)
{
    $function = 'foo';
    $bar->{$function}(1, 2, 3);
    $i++;
}
echo microtime(true) - $time;
echo '<br />';echo '<br />';


echo '$class::$function(1, 2, 3)';echo '<br />';
$i = 0;
$class = 'Bar';
$function = 'static_foo';
$time = microtime(true);
while ($i<$max)
{
    $class::$function(1, 2, 3);
    $i++;
}
echo microtime(true) - $time;
echo '<br />';echo '<br />';
?>



---------------------------------------------

Majd egy nálam produkált egyik kimenet:


1 - 10000

foo(1, 2, 3)
0.1220018863678

$foo = function($a, $b, $c){ /* ... */ }
$foo(1,2,3)
0.23000001907349

$function = "foo";
$function(1, 2, 3)
0.13000011444092

call_user_func('foo', 1, 2, 3)
0.21000099182129

call_user_func_array('foo', array(1, 2, 3))
0.22499990463257

call_user_func(array('Bar', 'foo'), 1,2,3)
0.27999997138977

call_user_func_array(array("Bar", "foo"), array(1,2,3))
0.29000091552734

call_user_func_array("Bar::foo"), array(1,2,3))
0.29000020027161

$bar->{$function}(1, 2, 3)
0.13499999046326

$class::$function(1, 2, 3)
0.13499999046326



---------------------------------------------

Kb. a dobogó:
  • 1. hely
    • foo(1, 2, 3)
  •  2. hely
    • $function = "foo"; $function(1, 2, 3)
    • $bar->{$function}(1, 2, 3)
    • $class::$function(1, 2, 3)
  • 3. hely:
    • call_user_*()

Nincsenek megjegyzések:

Megjegyzés küldése