note 123204 added to function.array-intersect
| From: | zoolyka at gmail dot com | Date: | Sat, 06 Oct 2018 07:38:40 +0000 |
| Subject: | note 123204 added to function.array-intersect | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-209380@lists.php.net to get a copy of this message | ||
If you have to intersect arrays of unique values then using array_intersect_key is about 20 times
faster, just have to flip the key value pairs of the arrays, then flip the result again.
<?php
$one = range(1, 250000);
$two = range(50000, 150000);
$start = microtime(true);
$intersection = array_intersect($one, $two);
echo "Did it in ".( microtime(true) - $start )." seconds.\n";
$start = microtime(true);
$intersection = array_flip(array_intersect_key(array_flip($one), array_flip($two)));
echo "Did it in ".( microtime(true) - $start )." seconds.\n";
?>
Did it in 0.89163708686829 seconds.
Did it in 0.038213968276978 seconds.
----
Server IP: 208.43.231.9
Probable Submitter: 88.132.196.188
----
Manual Page -- http://php.net/manual/en/function.array-intersect.php
Edit -- https://master.php.net/note/edit/123204
Del: integrated -- https://master.php.net/note/delete/123204/integrated
Del: useless -- https://master.php.net/note/delete/123204/useless
Del: bad code -- https://master.php.net/note/delete/123204/bad+code
Del: spam -- https://master.php.net/note/delete/123204/spam
Del: non-english -- https://master.php.net/note/delete/123204/non-english
Del: in docs -- https://master.php.net/note/delete/123204/in+docs
Del: other reasons-- https://master.php.net/note/delete/123204
Reject -- https://master.php.net/note/reject/123204
Search -- https://master.php.net/manage/user-notes.php