Calling global function vs calling static method

Submitted by tomek on Sun, 05/08/2011 - 13:42

f1 is slower than f2 by 85.19%

Full source code as executed.
define('LOOP',1000000);
 
class ClassA {
  static function a() {
  }
}
 
function b() {
}
 
function f1() {
  for($i=0; $i<LOOP; ++$i) {
    ClassA::a();  
    ClassA::a();  
    ClassA::a();  
    ClassA::a();  
    ClassA::a();  
    ClassA::a();  
    ClassA::a();  
    ClassA::a();  
    ClassA::a();  
    ClassA::a();  
  }
}
 
function f2() {
  for($i=0; $i<LOOP; ++$i) {
    b();
    b();
    b();
    b();
    b();
    b();
    b();
    b();
    b();
    b();
  }
}
 
$start = microtime(true);
f1();
$stop = microtime(true);
$time1 = $stop - $start;
 
$start = microtime(true);
f2();
$stop = microtime(true);
$time2 = $stop - $start;
 
echo $time1 . "\t";
echo $time2 . "\n";
Test results
comparisontime1time2php_version
110.93%721961334228005.3.5-1ubuntu7.2
141.83%637234126350755.3.3-7
175.88%1126380740828715.3.5-1ubuntu7.2
125.71%644206528541035.3.3-7+squeeze1
117.61%1042530479076php-5.3.6-phpbrew_compiled
124.15%981373437819php-5.3.7-phpbrew_compiled
126.16%1015095448834php-5.3.8-phpbrew_compiled
124.54%999260445027php-5.3.9-phpbrew_compiled
150.87%1104135440122php-5.3.10-phpbrew_compiled
127.05%1002800441669php-5.3.11-phpbrew_compiled
128%1000379438754php-5.3.12-phpbrew_compiled
127.27%1001003440438php-5.3.13-phpbrew_compiled
123.91%971848434038php-5.3.14-phpbrew_compiled
124.42%1015118452337php-5.3.15-phpbrew_compiled
125.09%1015336451088php-5.3.16-phpbrew_compiled
123.11%1005029450467php-5.3.17-phpbrew_compiled
130.39%1026574445585php-5.3.18-phpbrew_compiled
118.41%1026847470146php-5.3.19-phpbrew_compiled
6.42%367674345494php-5.4.0-phpbrew_compiled
6.99%389387363931php-5.4.1-phpbrew_compiled
6.13%380584358587php-5.4.2-phpbrew_compiled
6.87%392035366842php-5.4.3-phpbrew_compiled
5.03%369919352202php-5.4.4-phpbrew_compiled
5.94%368474347818php-5.4.5-phpbrew_compiled
6.68%374102350662php-5.4.6-phpbrew_compiled
5.64%372444352575php-5.4.7-phpbrew_compiled
5.12%376348358006php-5.4.8-phpbrew_compiled
5.1%380433361980php-5.4.9-phpbrew_compiled
50%
updown
50%