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;
- }
- ?>
-
The implementation of the above code is a little far-fetched. 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;
- }
- ?>
-
This is almost comparable to the speed of the original array_diff function. But is there any better way to optimize it? I found 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, and the Value is only stored by the Key organization, there is no index, and each search is traversed. Although this is a tip of the PHP language, 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. The complete code is 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 => $ 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 ";
- ?>
|