-
-
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;
- }
- ?>
- < ;/p>
-
Copy code
The implementation of the above code is a bit far-fetched.
So I reconsidered it and optimized the algorithm. The second function looked 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 code
This time it is almost as fast as the original array_diff function.
But is there a more optimized way?
I found that PHP can be written 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;
- }
- ?>
-
-
Copy code
The efficiency of this function is amazing, even faster than the original array_diff function. Investigating the reason, I found the explanation:
Because the keys are HASH organized, the search is fast;
Value is only organized and stored by Key. It has no index itself, and each search is traversed. Summarize
Although this is a little trick of the PHP language, when traversing and comparing array values, if you need to compare the value, reversing it with the key is indeed much more efficient than the usual value-to-value comparison.
For example, function two above needs to call the in_array function and needs to loop to determine whether it is within the function; while function three only determines whether the key exists in the array. Coupled with the different organizational indexing methods of array keys and values, it is very understandable that the efficiency is higher than imagined.
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;
- }< /p>
- 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 runs" . (microtime_float() - $time_start) . " seconds n";
$time_start = microtime_float ();
- array_diff3($ary_1, $ary_2);
- echo "Function array_diff3 runs" . (microtime_float() - $time_start) . "Seconds";
$time_start = microtime_float( );
- array_diff4($ary_1, $ary_2);
- echo "Function array_diff4 run" . (microtime_float() - $time_start) . "Seconds";
- ?>
-
Copy code
|