note 27952 added to function.in-array
| From: | melissa at hotmail dot com | Date: | Fri, 27 Dec 2002 06:53:27 +0000 |
| Subject: | note 27952 added to function.in-array | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-41337@lists.php.net to get a copy of this message | ||
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";
//=============================================
--
http://www.php.net/manual/en/function.in-array.php
http://master.php.net/manage/user-notes.php?action=edit+27952
http://master.php.net/manage/user-notes.php?action=delete+27952
http://master.php.net/manage/user-notes.php?action=reject+27952