Skip to content

Instantly share code, notes, and snippets.

@taai
Created August 3, 2012 13:23
Show Gist options
  • Save taai/3247681 to your computer and use it in GitHub Desktop.
Save taai/3247681 to your computer and use it in GitHub Desktop.
Don't use call_user_func() when you can live without it. I's 4-8 times slower!
<?php
class Employee {
public static function salary()
{
return 0;
}
public function test1()
{
$self = get_class($this);
return call_user_func(array($self, 'salary'));
}
public function test2()
{
return static::salary();
}
public function test3($true = TRUE)
{
if ($true)
{
call_user_func_array(array($this, __FUNCTION__), array( ! $true));
}
}
public function test4($true = TRUE)
{
if ($true)
{
$this->test4( ! $true);
}
}
}
class Designer extends Employee {
public static function salary()
{
return 1000;
}
}
$designer = new Designer;
// Test 1
$start = microtime(true);
for ($i = 0; $i < 1000000; $i++)
{
$designer->test1();
}
$end = microtime(true);
echo 'Test 1 time: ', $end - $start, '<br>';
// Test 2
$start = microtime(true);
for ($i = 0; $i < 1000000; $i++)
{
$designer->test2();
}
$end = microtime(true);
echo 'Test 2 time: ', $end - $start, '<br>';
// Test 3
$start = microtime(true);
for ($i = 0; $i < 1000000; $i++)
{
$designer->test3();
}
$end = microtime(true);
echo 'Test 3 time: ', $end - $start, '<br>';
// Test 4
$start = microtime(true);
for ($i = 0; $i < 1000000; $i++)
{
$designer->test4();
}
$end = microtime(true);
echo 'Test 4 time: ', $end - $start, '<br>';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment