Type hinting slowing script down

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

f1 is slower than f2 by 67.64%

Full source code as executed.
define('LOOP',1000000);
 
class ClassA {
}
 
function a($arg1, ClassA $arg2=null, ClassA $arg3=null, ClassA $arg4=null) {};
function b($arg1, $arg2=null, $arg3=null, $arg4=null) {};
 
function f1() {
  $a = new ClassA();
  for($i=0; $i<LOOP; ++$i) {
    a(1, $a, $a, $a); 
  }
}
 
function f2() {
  $a = new ClassA();
  for($i=0; $i<LOOP; ++$i) {
    b(1, $a, $a, $a);
  }
}
 
$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
65.64%264454015965945.3.5-1ubuntu7.2
49.1%235528415796235.3.3-7
72.07%424769124686035.3.5-1ubuntu7.2
60.78%233436614518785.3.3-7+squeeze1
67.82%443875264497php-5.3.6-phpbrew_compiled
61.92%396204244695php-5.3.7-phpbrew_compiled
60.27%414049258346php-5.3.8-phpbrew_compiled
58.97%397258249891php-5.3.9-phpbrew_compiled
62.44%397117244465php-5.3.10-phpbrew_compiled
61.47%399279247285php-5.3.11-phpbrew_compiled
61.56%403201249568php-5.3.12-phpbrew_compiled
61.45%399471247420php-5.3.13-phpbrew_compiled
77.61%437296246216php-5.3.14-phpbrew_compiled
59.72%396645248337php-5.3.15-phpbrew_compiled
57.2%417071265309php-5.3.16-phpbrew_compiled
62.01%396276244602php-5.3.17-phpbrew_compiled
56.2%413108264469php-5.3.18-phpbrew_compiled
74.46%450080257991php-5.3.19-phpbrew_compiled
87.13%348134186034php-5.4.0-phpbrew_compiled
69.83%349649205883php-5.4.1-phpbrew_compiled
81.14%338575186916php-5.4.2-phpbrew_compiled
69.78%353696208324php-5.4.3-phpbrew_compiled
77.06%331517187232php-5.4.4-phpbrew_compiled
77.3%335933189472php-5.4.5-phpbrew_compiled
71.19%330368192978php-5.4.6-phpbrew_compiled
78.4%336579188661php-5.4.7-phpbrew_compiled
78.95%335537187504php-5.4.8-phpbrew_compiled
72.51%350200202998php-5.4.9-phpbrew_compiled
50%
updown
50%