note 27952 deleted from function.in-array by danbrown
| From: | danbrown@php.net | Date: | Mon, 08 Jun 2009 21:47:08 +0000 |
| Subject: | note 27952 deleted from function.in-array by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-156530@lists.php.net to get a copy of this message | ||
Note Submitter: melissa at hotmail dot com
----
With a bit of testing I've found this function to be quite in-efficient...
To demonstrate... I tested 30000 lookups in a consistant environment. Using an internal stopwatch
function I got approximate time of over 1.5 mins using the in_array function.
However, using an associative array this time was reduced to less than 1 second...
In short... Its probably not a good idea to use in_array on arrays bigger than a couple of
thousand...
The growth is exponential...
values
in_array assocative array
1000 00:00.05 00:00.01
10000 00:08.30 00:00.06
30000 01:38.61 00:00.28
100000 ...(over 15mins).... 00:00.64
Example Code... test it out for yourself...:
//=============================================
$Count = 0;
$Blah = array();
while($Count<30000)
{
if(!$Blah[$Count])
$Blah[$Count]=1;
$Count++;
}
echo "Associative Array";
$Count = 0;
$Blah = array();
while($Count<30000)
{
if(!in_array($Count, $Blah))
$Blah[] = $Count;
$Count++;
}
echo "In_Array";
//=============================================