PhpRiot
News Archive
PhpRiot Newsletter
Your Email Address:

More information

Performance Analysis of isset() vs array_key_exists()

Note: This article was originally published at Planet PHP on 4 March 2012.
Planet PHP

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: =Aarray(); $fpA=Afopen("/usr/share/dict/words",A"r"); whileA($iA5000A&&A($wA=Afgets($fp)))A{ AAAA$arr[trim($w)]A=A++$i; } $sA=Amicrotime(1); forA($iA=A0;A$iA100000;A$i++)A{ AAAAisset($arr['abracadabra']); } $eA=Amicrotime(1); echoA"Isset:A".($eA-A$s)."\n"; $sA=Amicrotime(1); forA($iA=A0;A$iA100000;A$i++)A{ AAAAarray_key_exists('abracadabra',A$arr); } $eA=Amicrotime(1); echoA"array_key_exists:A".($eA-A$s"/

Truncated by Planet PHP, read more at the original (another 1162 bytes)