isset vs array_key_exists

Submitted by tomek on Sun, 05/08/2011 - 14:02

f2 is slower than f1 by 374.54% (3.7 times slower)

Full source code as executed.
define('LOOP',1000000);
function f1() {
  $a = array('abc'=>17);
  for($i=0; $i<LOOP; ++$i) {
    isset($a['abc']);
    isset($a['abc']);
    isset($a['abc']);
    isset($a['abc']);
    isset($a['abc']);
  }
}
function f2() {
  $a = array('abc'=>17);
  for($i=0; $i<LOOP; ++$i) {
    array_key_exists('abc',$a);
    array_key_exists('abc',$a);
    array_key_exists('abc',$a);
    array_key_exists('abc',$a);
    array_key_exists('abc',$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
387.89%116465856821925.3.5-1ubuntu7.2
536.23%87599655733715.3.3-7
467.82%149643584971035.3.5-1ubuntu7.2
499.35%86966152123465.3.3-7+squeeze1
352.22%152894691422php-5.3.6-phpbrew_compiled
327%163356697534php-5.3.7-phpbrew_compiled
345.68%156840699005php-5.3.8-phpbrew_compiled
338.84%156537686952php-5.3.9-phpbrew_compiled
341.77%154187681157php-5.3.10-phpbrew_compiled
337.58%153958673695php-5.3.11-phpbrew_compiled
337.91%153937674111php-5.3.12-phpbrew_compiled
338.3%153853674344php-5.3.13-phpbrew_compiled
356.8%150090685614php-5.3.14-phpbrew_compiled
354.16%152445692337php-5.3.15-phpbrew_compiled
344.91%151536674196php-5.3.16-phpbrew_compiled
351.99%152146687687php-5.3.17-phpbrew_compiled
289.89%178297695169php-5.3.18-phpbrew_compiled
345.97%151448675412php-5.3.19-phpbrew_compiled
235.02%151870508793php-5.4.0-phpbrew_compiled
404.62%99489502043php-5.4.1-phpbrew_compiled
409.75%99773508594php-5.4.2-phpbrew_compiled
401.26%101951511043php-5.4.3-phpbrew_compiled
407.19%100851511508php-5.4.4-phpbrew_compiled
422.8%98803516546php-5.4.5-phpbrew_compiled
408.7%99884508111php-5.4.6-phpbrew_compiled
395.5%102160506207php-5.4.7-phpbrew_compiled
405.57%98398497473php-5.4.8-phpbrew_compiled
342.32%116038513261php-5.4.9-phpbrew_compiled
50%
updown
50%

Comments