PHP array intersection optimization. PHP array intersection optimization suppose we are operating a mobile phone-related website. Users can screen PHP array intersection optimization by specifying several parameters (such as operating system, screen resolution, camera pixel, and so on ).
Suppose we are running a mobile phone-related website. you can specify several parameters (such as operating system, screen resolution, camera pixel, and so on) to filter your desired mobile phone. However, because mobile phones have many parameters and their parameters vary greatly, the parameter table structure is generally a vertical table (one parameter is a row ), instead of a horizontal table (a parameter is a column), several parameters are used to obtain the result. Generally, each individual parameter is used to obtain the result and the intersection is obtained together.
Assuming that each parameter contains about one thousand unique results (id int), the following data is generated on the premise that:
$rand = function() { $result = array(); for ($i = 0; $i < 1000; null) { $value = mt_rand(1, 10000); if (!isset($result[$value])) { $result[$value] = null; $i++; } } return array_keys($result); }; $param_a = $rand(); $param_b = $rand(); ?> |
Note: If the test dataset is too small, the conclusions may be inconsistent. First, let's look at the performance achieved through the PHP built-in method array_intersect:
$time = microtime(true); $result = array_intersect($param_a, $param_b); $time = microtime(true) - $time; echo "array_intersect: {$time}n"; ?> |
Let's take a look at the performance achieved through the custom method intersect:
function intersect() { if (func_num_args() < 2) { trigger_error('param error', E_USER_ERROR); } $args = func_get_args(); foreach ($args AS $arg) { if (!is_array($arg)) { trigger_error('param error', E_USER_ERROR); } } $intersect = function($a, $b) { $result = array(); $length_a = count($a); $length_b = count($b); for ($i = 0, $j = 0; $i < $length_a && $j < $length_b; null) { if($a[$i] < $b[$j]) { $i++; } else if($a[$i] > $b[$j]) { $j++; } else { $result[] = $a[$i]; $i++; $j++; } } return $result; }; $result = array_shift($args); sort($result); foreach ($args as $arg) { sort($arg); $result = $intersect($result, $arg); } return $result; } $time = microtime(true); $result = intersect($param_a, $param_b); $time = microtime(true) - $time; echo "intersect: {$time}n"; ?> |
Intuitively, we will certainly think that built-in functions are faster than custom functions, but the results in this example are the opposite:
Array_intersect: 0.023918151855469
Intersect: 0.0026049613952637
Please note that the functions of array_intersect and intersect are not fully equivalent. The example is as follows:
$param_a = array(1, 2, 2); $param_b = array(1, 2, 3); var_dump( array_intersect($param_a, $param_b), intersect($param_a, $param_b) ); |
Array_intersect: 1, 2, 2
Intersect: 1, 2
That is to say, if there are repeated elements in the first array parameter, array_intersect will return all repeated elements that meet the conditions, instead of simply returning one, if you are interested, you can change the parameter order and then view the result.
When I first wrote the intersect method, I wrote it like the following:
function intersect() { if (func_num_args() < 2) { trigger_error('param error', E_USER_ERROR); } $args = func_get_args(); foreach ($args AS $arg) { if (!is_array($arg)) { trigger_error('param error', E_USER_ERROR); } } $result = array(); $data = array_count_values( call_user_func_array('array_merge', $args) ); foreach ($data AS $value => $count) { if ($count > 1) { $result[] = $value; } } return $result; } ?> |
The code is more concise, but there is a drawback. because array_merge is used, when there are many elements in the array, the memory occupied will be relatively large. Otherwise, if there are not many elements in the array, this method is also feasible.
Reference: Faster array_intersect
If we are running a mobile phone-related website, you can screen the website by specifying several parameters (such as operating system, screen resolution, camera pixels, and so on...