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 => $ v1 ){
$ Flag = false;
Foreach ($ array_2 as $ v2 ){
If ($ flag = ($ v1 = $ v2 )){
Break;
}
}

If (! $ Flag ){
$ Diff [$ k] = $ v1;
}
}

Return $ diff;
} Although the implementation is acceptable, the efficiency of this function is terrible. So I reconsider and optimized the algorithm. 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 is almost comparable to the speed of the original array_diff function. But is there any better way to optimize it? I wrote an article on ChinaUnix (sorry, I cheated). I found that PHP could write like 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 very efficient, even faster than the original array_diff function. The reason is as follows:

Because keys are organized by HASH, the search is fast;
The Value is only stored by the Key organization, and there is no index in itself. Every search is traversed. Summary
This is a tip of the PHP language, but in terms of traversing and comparing the values of the array, if you need to compare the value with the key reversal, it is indeed much more efficient than the normal value pair.

For example, function 2 needs to call the in_array function to determine whether it is in the function cyclically. function 3 only checks whether the array exists with the key. With the array keys and values in different organization indexes, the efficiency is much higher than expected.

Code attachment
Copy codeThe Code is 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 => $ v1 ){
$ Flag = false;
Foreach ($ array_2 as $ v2 ){
If ($ flag = ($ v1 = $ v2 )){
Break;
}
}

If (! $ Flag ){
$ Diff [$ k] = $ v1;
}
}

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 \ 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 ";
?>


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.