Php calculates the custom function of the array difference set (the efficiency is better than the array_diff function)
| This article introduces the array_different function of a custom array difference set, which is more efficient than the array_diff function. For more information, see. Php calculates the array difference set. the code is as follows:
$ Val) {if (isset ($ array_2 [$ val]) {unset ($ array_1 [$ key]) ;}return $ array_1 ;} function runtime ($ mode = 0) {static $ t; if (! $ Mode) {$ t = microtime (); return ;}$ t1 = microtime (); list ($ m0, $ s0) = explode ("", $ t ); list ($ m1, $ s1) = explode ("", $ t1); return sprintf ("%. 3f ", ($ s1 + $ m1-$ s0-$ m0) * 1000);} $ array_1 = array (); $ array_2 = array (); for ($ I = 0; $ I <= 5000; $ I ++) {$ array_1 [$ I] = mt_rand (0,100 ); $ array_2 [$ I] = mt_rand (0,100);} runtime (); // Start Time $ arr_diff = array_diff ($ array_1, $ array_2); echo runtime (1 ); // The timer ends and outputs the timer result echo' '; Foreach ($ arr_diff as $ key => $ val) {echo $ val. ',';} runtime (); // timing start $ arr_diff2 = array_different ($ array_1, $ array_2); echo runtime (2 ); // The timer ends and outputs the timer result foreach ($ arr_diff2 as $ key => $ val) {echo $ val. ',' ;}?> |