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;
- }
- ?>
Copy CodeThe implementation of the above code is a bit farfetched. 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;
- }
- ?>
Copy CodeThis time is almost comparable to the speed of the original Array_diff function. But is there any way to optimize it? Find PHP can write this way:
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;
- }
- ?>
Copy CodeThis function is surprisingly efficient, even faster than the original Array_diff function. The reason, I found the explanation: Because the keys are organized in a HASH, the lookup is quick, and Value is stored by the key organization, which itself is not indexed, and each lookup is traversed. Summarizing this is a small trick of the PHP language, but in traversing and contrasting the values of the array, it is much more efficient to compare the value to the key than the usual value if it is necessary to reverse the value. 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. 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 = + $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";
- ?>
Copy Code |