note 81177 deleted from function.in-array by danbrown
| From: | danbrown@php.net | Date: | Mon, 08 Jun 2009 21:41:36 +0000 |
| Subject: | note 81177 deleted from function.in-array by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-156506@lists.php.net to get a copy of this message | ||
Note Submitter: ucffool
----
@vidmantas
I had different results... Here is an example:
<?php
$array = array();
for ($x=1;$x <= 1000;$x++):
$array[] = mt_rand(100000,999999);
endfor;
$value = 123456;
$time=microtime(1);
if (in_array($value,$array)):
echo "Found! ";
else:
echo "Not found! ";
endif;
$time=number_format(microtime(1)-$time, 6);
echo "Time to complete: " . $time;
echo "<br />"; // -------------------------------------
$time=microtime(1);
$array2 = array_flip($array);
if (isset($array2[$value])):
echo "Found! ";
else:
echo "Not found! ";
endif;
$time=number_format(microtime(1)-$time, 6);
echo "Time to complete: " . $time;
?>
RESULT:
Not found! Time to complete: 0.000075
Not found! Time to complete: 0.000259
So I would say that on PHP5 at least, in_array seems to be more effective.