note 55923 added to function.array-unique
| From: | muddmonkey at harveyMcoldotedu | Date: | Wed, 17 Aug 2005 20:44:10 +0000 |
| Subject: | note 55923 added to function.array-unique | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-93808@lists.php.net to get a copy of this message | ||
If you're doing numeric arrays etc. I found flip-flip to work much better than array_unique:
<?PHP
function microtime_float(){ //timing
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
//make an arry and fill it up
$final=array();
for($i=0;$i<50000;$i++){
$final[]=$i%13; //make sure there are some dupes
}
//try array unique
$start1 = microtime_float();
array_unique($final);
$stop1=microtime_float();
echo($stop1-$start1.'<br>');
//try my flip-flip
$start2=microtime_float();
array_flip(array_flip($final));
$stop2=microtime_float();
echo($stop2-$start2);
?>
Running this with only ints in the array (as above) I get runtimes such as:
1.6195669174194 (using unique)
0.017037868499756 (using flip flip)
which is two orders of magnitude faster!
Appending a string:
($final[]='test'.$i%13;)
gives:
0.42909598350525 (using unique)
0.023258924484253 (using flip-flip)
Which is not AS great, but still 20x faster than unique.
In both cases the flip-flip seems to use less memory than the unique.
Granted the flip-flip doesn't work for all cases, but if you're doing simple stuff like
this, the flip-flip will give you better run times.
~JF
----
Manual Page -- http://www.php.net/manual/en/function.array-unique.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+55923
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+55923&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+55923&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+55923&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+55923&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+55923&report=yes&reason=non-english
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+55923&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+55923&report=yes
Search -- http://master.php.net/manage/user-notes.php