PHP array intersection optimization code analysis
- 2020-03-31 21:35:14
- OfStack
However, because there are many parameters of mobile phones, and the parameters of different mobile phones vary greatly, the structure of parameter table is usually a vertical table (one parameter is a row), rather than a horizontal table (one parameter is a column). At this time, a number of parameters are used to get the result, usually each single parameter to get the result, and then the intersection.
Assuming that each parameter will contain a thousand or so unique results (id int), this is the premise to simulate the generation of some data:
Note: conclusions can be inconsistent if the test data set is too small. Let's take a look at the performance of the array_intersect implementation using PHP's built-in method:
Now let's look at the performance of the intersect implementation using a custom method:
Intuitively, we would think that built-in functions are faster than custom functions, but in this case the opposite is true:
The array_intersect: 0.023918151855469
Intersects: 0.0026049613952637
It is important to note that array_intersect and intersect are not functionally equivalent. Here is an example:
Array_intersect: 1, 2, 2
Intersect: 1, 2
That is, if there are duplicate elements in the first array parameter, array_intersect will return all the duplicate elements that satisfy the condition, instead of just one.
And just to nag you, when I first wrote intersect, I wrote something like this:
The code is cleaner, but there is a drawback, because array_merge is used, so it takes up a lot of memory when there are a lot of elements in the array, and it works if there are not a lot of elements in the array. (credit: hot notes)
Assuming that each parameter will contain a thousand or so unique results (id int), this is the premise to simulate the generation of some data:
<?php
$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: conclusions can be inconsistent if the test data set is too small. Let's take a look at the performance of the array_intersect implementation using PHP's built-in method:
<?php
$time = microtime(true);
$result = array_intersect($param_a, $param_b);
$time = microtime(true) - $time;
echo "array_intersect: {$time}n";
?>
Now let's look at the performance of the intersect implementation using a custom method:
<?php
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 would think that built-in functions are faster than custom functions, but in this case the opposite is true:
The array_intersect: 0.023918151855469
Intersects: 0.0026049613952637
It is important to note that array_intersect and intersect are not functionally equivalent. Here is an example:
$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, if there are duplicate elements in the first array parameter, array_intersect will return all the duplicate elements that satisfy the condition, instead of just one.
And just to nag you, when I first wrote intersect, I wrote something like this:
<?php
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 cleaner, but there is a drawback, because array_merge is used, so it takes up a lot of memory when there are a lot of elements in the array, and it works if there are not a lot of elements in the array. (credit: hot notes)