Pondering the difference of PHP array traversal (ARRAY_DIFF) _php skills

Source: Internet
Author: User
Tags diff php language rand
function Array_diff ($array _1, $array _2) {
$diff = Array ();

foreach ($array _1 as $k => $v 1) {
$flag = false;
foreach ($array _2 as $v 2) {
if ($flag = ($v 1 = $v 2)) {
Break
}
}

if (! $flag) {
$diff [$k] = $v 1;
}
}

return $diff;
Although implementation is possible, discovering the efficiency of this function is appalling. So I reconsidered and optimized the algorithm, and the second function looked like this:

function Array_diff ($array _1, $array _2) {
foreach ($array _1 as $key => $item) {
if (In_array ($item, $array _2, True)) {
unset ($array _1[$key]);
}
}

return $array _1;
Well, this time it's almost comparable to the speed of the original Array_diff function. But is there a more optimized approach? By an article on Chinaunix (sorry, cheating), I found that PHP can actually write this:

function Array_diff ($array _1, $array _2) {
$array _2 = Array_flip ($array _2);
foreach ($array _1 as $key => $item) {
if (Isset ($array _2[$item])) {
unset ($array _1[$key]);
}
}

return $array _1;
This function is incredibly efficient, even faster than the original Array_diff function. The reason, I found an explanation:

Because the key is to HASH organization, find quickly;
And Value is only stored by the Key organization, there is no index itself, each lookup is traversed. Summarize
While this is a small trick in the PHP language, it is much more efficient to iterate over the values of the array than to reverse the value by comparing it with the key.

For example, the function two above needs to call the In_array function to loop to determine whether it is in the function, and the function three simply determines whether the array exists. With the array key and the value of different organization index way, the efficiency is higher than the imagination that is very understandable.

Attached code
Copy Code code as follows:

<?php
function Microtime_float () {
List ($usec, $sec) = Explode ("", Microtime ());
Return ((float) $usec + (float) $sec);
}

function array_diff2 ($array _1, $array _2) {
$diff = Array ();

foreach ($array _1 as $k => $v 1) {
$flag = false;
foreach ($array _2 as $v 2) {
if ($flag = ($v 1 = $v 2)) {
Break
}
}

if (! $flag) {
$diff [$k] = $v 1;
}
}

return $diff;
}


function array_diff3 ($array _1, $array _2) {
foreach ($array _1 as $key => $item) {
if (In_array ($item, $array _2, True)) {
unset ($array _1[$key]);
}
}

return $array _1;
}


function Array_diff4 ($array _1, $array _2) {
$array _2 = Array_flip ($array _2);
foreach ($array _1 as $key => $item) {
if (Isset ($array _2[$item])) {
unset ($array _1[$key]);
}
}

return $array _1;
}

//////////////////////////////

for ($i = 0, $ary _1 = Array (); $i < 5000; $i + +) {
$ary _1[] = rand (100, 999);
}

for ($i = 0, $ary _2 = Array (); $i < 5000; $i + +) {
$ary _2[] = rand (100, 999);
}

Header ("Content-type:text/plain;charset=utf-8");

$time _start = Microtime_float ();
Array_diff ($ary _1, $ary _2);
echo "function Array_diff run". (Microtime_float ()-$time _start). "Seconds \";

$time _start = Microtime_float ();
ARRAY_DIFF2 ($ary _1, $ary _2);
echo "function array_diff2 run". (Microtime_float ()-$time _start). "Seconds \";

$time _start = Microtime_float ();
ARRAY_DIFF3 ($ary _1, $ary _2);
echo "function array_diff3 run". (Microtime_float ()-$time _start). "Seconds \";

$time _start = Microtime_float ();
Array_diff4 ($ary _1, $ary _2);
echo "function Array_diff4 run". (Microtime_float ()-$time _start). "Seconds \";
?>


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.