note 90329 deleted from function.array-key-exists by danbrown
| From: | danbrown@php.net | Date: | Thu, 16 Apr 2009 15:55:02 +0000 |
| Subject: | note 90329 deleted from function.array-key-exists by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-153395@lists.php.net to get a copy of this message | ||
Note Submitter: manixrock-~-gmail-~-com
----
@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.