note 110583 deleted from function.array-diff by danbrown
| From: | danbrown@php.net | Date: | Thu, 06 Dec 2012 14:20:08 +0000 |
| Subject: | note 110583 deleted from function.array-diff by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-192323@lists.php.net to get a copy of this message | ||
Note Submitter: Jppe Utzon
----
I should add that wrt. my post just below, my alternative method for evaluating if two arrays
contain (all) identical values:
<?php
sort($a); $sort(b); return $a == $b;
?>
may be slightly faster (10-20%) than this array_diff method:
<?php
return ( count( $a ) == count( $b ) && !array_diff( $a , $b ) ? true : false );
?>
but only when the two arrays contain the same number of values and then only in some cases.
Otherwise the latter method will be radically faster due to the use of a count() test before the
array_diff().
Also, if the two arrays contain a different number of values, then which method is faster will
depend on whether both arrays need to be sorted or not. Two times sort() is a bit slower than one
time array_diff(), but if one of the arrays have already been sorted, then you only have to sort the
other array and this will be almost twice as fast as array_diff().
Basically: 2 x sort() is slower than 1 x array_diff() is slower than 1 x sort().