note 90329 added to function.array-key-exists

From: Date: Thu, 16 Apr 2009 15:46:12 +0000
Subject: note 90329 added to function.array-key-exists
Groups: php.notes 
Request: Send a blank email to php-notes+get-153394@lists.php.net to get a copy of this message
@tmont Your isset() vs. array_keys_exist() benchmark is inaccurate because while it does show clearly which is faster it doesn't accurately shows just how much faster it is because you're also measuring the time it takes for the loop to run and the array insertion functions. So I did one that measures time more accurately: <?php $times = 1000000; $arr = array_fill(0, $times/2, 'Why hello thar!'); $T0 = microtime(true); for ($i=0; $i<$times; $i++); $T1 = microtime(true); for ($i=0; $i<$times; $i++) isset($arr[$i]); $T2 = microtime(true); for ($i=0; $i<$times; $i++) array_key_exists($i, $arr); $T3 = microtime(true); $Tloop = $T1-$T0; // correct for time taken by the loop commands (increasing $i and checking $1<10000) $Tiss = $T2-$T1-$Tloop; $Take = $T3-$T2-$Tloop; echo $times.' isset() calls: '.round($Tiss, 3).'s ('.round(100*$Tiss/($T3-$T0), 2).'%)<br>'; echo $times.' array_key_exists() calls: '.round($Take, 3).'s ('.round(100*$Take/($T3-$T0), 2).'%)<br>'; echo 'loop calls: '.round($Tloop*3, 3).'s ('.round(100*$Tloop*3/($T3-$T0), 2).'%)<br>'; ?> The results I got on my computer: 1000000 isset() calls: 0.146s (10.52%) 1000000 array_key_exists() calls: 0.539s (38.92%) loop calls: 0.7s (50.56%) So isset() is about 3.7 times faster than array_key_exists() on windows. ---- Server IP: 69.147.83.197 Probable Submitter: 78.96.116.14 ---- Manual Page -- http://www.php.net/manual/en/function.array-key-exists.php Edit -- https://master.php.net/note/edit/90329 Del: integrated -- https://master.php.net/note/delete/90329/integrated Del: useless -- https://master.php.net/note/delete/90329/useless Del: bad code -- https://master.php.net/note/delete/90329/bad+code Del: spam -- https://master.php.net/note/delete/90329/spam Del: non-english -- https://master.php.net/note/delete/90329/non-english Del: in docs -- https://master.php.net/note/delete/90329/in+docs Del: other reasons-- https://master.php.net/note/delete/90329 Reject -- https://master.php.net/note/reject/90329 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#153394) next »