note 110583 added to function.array-diff
| From: | JppeUtzon | Date: | Fri, 09 Nov 2012 13:48:05 +0000 |
| Subject: | note 110583 added to function.array-diff | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-191824@lists.php.net to get a copy of this message | ||
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().
----
Server IP: 195.184.96.15
Probable Submitter: 85.233.233.180
----
Manual Page -- http://www.php.net/manual/en/function.array-diff.php
Edit -- https://master.php.net/note/edit/110583
Del: integrated -- https://master.php.net/note/delete/110583/integrated
Del: useless -- https://master.php.net/note/delete/110583/useless
Del: bad code -- https://master.php.net/note/delete/110583/bad+code
Del: spam -- https://master.php.net/note/delete/110583/spam
Del: non-english -- https://master.php.net/note/delete/110583/non-english
Del: in docs -- https://master.php.net/note/delete/110583/in+docs
Del: other reasons-- https://master.php.net/note/delete/110583
Reject -- https://master.php.net/note/reject/110583
Search -- https://master.php.net/manage/user-notes.php