COleSafeArray pondering the differences in PHP array traversal (Implementation of Array_diff)

Source: Internet
Author: User
Tags php language
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 the implementation is possible, the efficiency of this function is found to be appalling. So I reconsidered and optimized the algorithm, and the second function looks 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 any way to optimize it? By an article on the Chinaunix (sorry, cheating), I found that PHP can actually write:
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 surprisingly efficient, even faster than the original Array_diff function. The reason, I found the explanation:
Because the key is the HASH organization, find quickly;
The Value is stored only by the Key organization and is not indexed by itself, and each lookup is traversed. Summarize
While this is a small trick of the PHP language, it is much more efficient to iterate over and contrast the values of the array than the normal value if it is necessary to reverse the value by comparing it to the key.
For example, the function two above needs to call the In_array function needs to loop to determine whether the function inside, and the function of the three only to determine whether the array exists the key is OK. With the array key and the value of the organization index of the different way, more efficient than the imagination that is very understandable.
Attached code

Copy the Code code as follows:


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 < $i + +) {
$ary _1[] = rand (100, 999);
}
for ($i = 0, $ary _2 = Array (), $i < $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 \ n";
$time _start = Microtime_float ();
ARRAY_DIFF2 ($ary _1, $ary _2);
echo "function array_diff2 run". (Microtime_float ()-$time _start). "Seconds \ n";
$time _start = Microtime_float ();
ARRAY_DIFF3 ($ary _1, $ary _2);
echo "function array_diff3 run". (Microtime_float ()-$time _start). "Seconds \ n";
$time _start = Microtime_float ();
Array_diff4 ($ary _1, $ary _2);
echo "function Array_diff4 run". (Microtime_float ()-$time _start). "Seconds \ n";
?>


The above describes the COleSafeArray thoughtful PHP array traversal differences (Array_diff implementation), including the colesafearray aspect of the content, I hope to be interested in PHP tutorial friends helpful.

  • 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.