Equal vs identical comparison operator

Submitted by tomek on Sun, 05/22/2011 - 18:38

f1 is slower than f2 by 165.16% (1.7 times slower)

Full source code as executed.
define('LOOP', 1000000);
$a = 17;
$b = "17";
function f1() {
  global $a, $b;
  for ($i = 0; $i < LOOP; ++$i) {
    if ($a == $b) {}
    if ($a == $b) {}
    if ($a == $b) {}
    if ($a == $b) {}
    if ($a == $b) {}
  }
}
function f2() {
  global $a, $b;
  for ($i = 0; $i < LOOP; ++$i) {
    if ($a === $b) {}
    if ($a === $b) {}
    if ($a === $b) {}
    if ($a === $b) {}
    if ($a === $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
126.39%16162537139115.3.5-1ubuntu7.2
124.44%14209556331045.3.3-7
165.19%412945415571635.3.5-1ubuntu7.2
121.51%14342676474985.3.3-7+squeeze1
155.08%257044100769php-5.3.6-phpbrew_compiled
159.75%25676898853php-5.3.7-phpbrew_compiled
158.68%264082102090php-5.3.8-phpbrew_compiled
160.47%262485100775php-5.3.9-phpbrew_compiled
152.02%253538100601php-5.3.10-phpbrew_compiled
115.6%255000118272php-5.3.11-phpbrew_compiled
116.25%255105117965php-5.3.12-phpbrew_compiled
115.63%255141118325php-5.3.13-phpbrew_compiled
156.14%256217100031php-5.3.14-phpbrew_compiled
157.66%259522100722php-5.3.15-phpbrew_compiled
154.14%259039101927php-5.3.16-phpbrew_compiled
153.47%255745100896php-5.3.17-phpbrew_compiled
147.77%278043112217php-5.3.18-phpbrew_compiled
163.17%264883100650php-5.3.19-phpbrew_compiled
206.34%23216675788php-5.4.0-phpbrew_compiled
147.7%23474694772php-5.4.1-phpbrew_compiled
165.46%25133094678php-5.4.2-phpbrew_compiled
147.79%23518994915php-5.4.3-phpbrew_compiled
219.79%23415773222php-5.4.4-phpbrew_compiled
246.8%24897371791php-5.4.5-phpbrew_compiled
249.79%25161471933php-5.4.6-phpbrew_compiled
211.7%23342374886php-5.4.7-phpbrew_compiled
235.73%23900771190php-5.4.8-phpbrew_compiled
190.09%25339687352php-5.4.9-phpbrew_compiled
50%
updown
50%