note 82867 added to function.array-key-exists

From: Date: Tue, 29 Apr 2008 21:54:24 +0000
Subject: note 82867 added to function.array-key-exists
Groups: php.notes 
Request: Send a blank email to php-notes+get-138918@lists.php.net to get a copy of this message
The argument of array_key_exists() vs. isset() came up in the workplace today, so I conducted a little benchmark to see which is faster: <?php // one-dimensional arrays $array = array_fill(0,50000,'tommy is the best!'); $arraykeyexists_result = array(); $start = microtime(true); for ($i = 0; $i < 100000; $i++) { if (array_key_exists($i,$array)) { $arraykeyexists_result[] = 1; } else { $arraykeyexists_result[] = 0; } } $arrtime = round(microtime(true)-$start,3); $start = microtime(true); for ($i = 0; $i < 100000; $i++) { if (isset($array[$i])) { $arraykeyexists_result[] = 1; } else { $arraykeyexists_result[] = 0; } } $istime = round(microtime(true)-$start,3); $totaltime = $arrtime+$istime; $arrpercentage = round(100*$arrtime/$totaltime,3); $ispercentage = round(100*$istime/$totaltime,3); echo "array_key_exists(): $arrtime [$arrpercentage%] seconds\n"; echo "isset(): $istime [$ispercentage%] seconds\n"; ?> On Windows, the output is similar to array_key_exists(): 0.504 [82.895%] seconds isset(): 0.104 [17.105%] seconds On Mac or Linux, isset() is faster but only by a factor of approximately 1.5. ---- Server IP: 209.41.74.194 Probable Submitter: 207.114.185.110 ---- Manual Page -- http://www.php.net/manual/en/function.array-key-exists.php Edit -- https://master.php.net/note/edit/82867 Del: integrated -- https://master.php.net/note/delete/82867/integrated Del: useless -- https://master.php.net/note/delete/82867/useless Del: bad code -- https://master.php.net/note/delete/82867/bad+code Del: spam -- https://master.php.net/note/delete/82867/spam Del: non-english -- https://master.php.net/note/delete/82867/non-english Del: in docs -- https://master.php.net/note/delete/82867/in+docs Del: other reasons-- https://master.php.net/note/delete/82867 Reject -- https://master.php.net/note/reject/82867 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#138918) next »