Performance Analysis of isset() vs array_key_exists()
At Confoo I had an interesting conversation with Guilherme Blanco regarding the fact that in Doctrine 2 they had a performance issue due to usage of array_key_exists() and how it was significantly slower than isset(). His anecdotal example was that doing isset() took 0.5 seconds, while array_key_exists() for the same operation took 5 minutes!
That seemed wrong, given that array_key_exists() simply does a hash table look-up, to determine if the array key exists and the only speed difference would be due to the fact that isset() is a language construct and does not have the overhead of function & parameter processing. So, I've decided to do a quick benchmark using a 5,000 element array.
PHP: =A array(); $fpA =A fopen("/usr/share/dict/words",A "r"); whileA ($iA 5000A &&A ($wA =A fgets($fp)))A { A A A A $arr[trim($w)]A =A ++$i; } $sA =A microtime(1); forA ($iA =A 0;A $iA 100000;A $i++)A { A A A A isset($arr['abracadabra']); } $eA =A microtime(1); echoA "Isset:A ".($eA -A $s)."\n"; $sA =A microtime(1); forA ($iA =A 0;A $iA 100000;A $i++)A { A A A A array_key_exists('abracadabra',A $arr); } $eA =A microtime(1); echoA "array_key_exists:A ".($eA -A $s"/Truncated by Planet PHP, read more at the original (another 1162 bytes)


