PHP static method slower than objet method
This is the PHP code...
class SomeClass
{
static function doTest($var) {return $var;}
}
class SomeObj
{
function doTest($var) {return $var;}
static function doTest2($var) {return $var;}
}
//bench static call
$starttime = microtime(true);
for ($i = 0; $i< 100000; $i++)
SomeClass::doTest($i);
echo "Static call time: " , (microtime(true)-$starttime) , " ms
";
//bench object method
$starttime = microtime(true);
$obj = new SomeObj();
for ($i = 0; $i< 100000; $i++)
$obj->doTest($i);
echo "Object method call time: " , (microtime(true)-$starttime) , " ms
";
//bench static call
$starttime = microtime(true);
for ($i = 0; $i< 100000; $i++)
SomeObj::doTest2($i);
echo "Static call (when the static is in the same class of object method) time: " , (microtime(true)-$starttime) , " ms
";
...and these are the results (simply reload page to do another test):
Static method call time: 0.049500226974487 ms
Object method call time: 0.035122871398926 ms
Static method call (when the stati method is in the same class of object method) time: 0.037359952926636 ms