is_a vs instanceof

Submitted by gekkie on Tue, 05/15/2012 - 15:25

f1 is slower than f2 by 195.31% (2 times slower)

Full source code as executed.
/* How many times the code under test should run in each function */
define('LOOP',1000000);
function f1() {
$a = new StdClass();
  for($i=0; $i<LOOP; ++$i) {
    is_a($a, 'StdClass');
  }
}
function f2() {
$a = new StdClass();
  for($i=0; $i<LOOP; ++$i) {
  $a instanceof StdClass;
  }
}
$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
178.73%15403355526265.3.3-7+squeeze1
116.74%21382298655php-5.3.6-phpbrew_compiled
116.9%20954296607php-5.3.7-phpbrew_compiled
115.55%21258098621php-5.3.8-phpbrew_compiled
136.74%23082197499php-5.3.9-phpbrew_compiled
117.74%21216697442php-5.3.10-phpbrew_compiled
117.12%21087697122php-5.3.11-phpbrew_compiled
116.92%21104397291php-5.3.12-phpbrew_compiled
116.51%21089797408php-5.3.13-phpbrew_compiled
122.04%21883398557php-5.3.14-phpbrew_compiled
86.32%216696116303php-5.3.15-phpbrew_compiled
68.04%217127129211php-5.3.16-phpbrew_compiled
122.98%21436896139php-5.3.17-phpbrew_compiled
63.24%216628132707php-5.3.18-phpbrew_compiled
114.22%219781102594php-5.3.19-phpbrew_compiled
355.75%16014035138php-5.4.0-phpbrew_compiled
240.39%17342550949php-5.4.1-phpbrew_compiled
357.45%19009941556php-5.4.2-phpbrew_compiled
242.08%17371250781php-5.4.3-phpbrew_compiled
343.7%15569435090php-5.4.4-phpbrew_compiled
341.84%15492635064php-5.4.5-phpbrew_compiled
335.47%15345035238php-5.4.6-phpbrew_compiled
345.46%15666435169php-5.4.7-phpbrew_compiled
351.17%15818935062php-5.4.8-phpbrew_compiled
259.6%17321448168php-5.4.9-phpbrew_compiled
50%
updown
50%