Home  >  Article  >  Backend Development  >  Optimized code analysis for PHP array intersection_PHP tutorial

Optimized code analysis for PHP array intersection_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:31:13852browse

However, because mobile phones have many parameters and the parameters of different mobile phones vary greatly, the parameter table structure is usually a vertical table (one parameter is one row) instead of a horizontal table (one parameter is one column). In this case, several parameters are used to obtain the results. , usually by taking each individual parameter to get the result, and then taking the intersection together.
Assume that each parameter will contain about a thousand unique results (id int), and use this as a premise to simulate generating some data:

Copy code The code is as follows:

$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 data set is too small, the conclusions may be inconsistent. Let’s first take a look at the performance achieved through the PHP built-in method array_intersect:
Copy code The code is as follows:

$time = microtime(true);
$result = array_intersect($param_a , $param_b);
$time = microtime(true) - $time;
echo "array_intersect: {$time}n";
?>

See again Look at the performance achieved through the custom method intersect:
Copy code The code is as follows:

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 definitely think that built-in functions are faster than custom functions, but in this case the result is just the opposite:
array_intersect: 0.023918151855469
intersect: 0.0026049613952637
I would like to remind everyone that array_intersect and intersect are not completely equivalent in function. Examples are as follows:
Copy code The code 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
In other words, if there are repeated elements in the first array parameter, array_intersect will return all repeated elements that meet the conditions, instead of just one. Interested readers can change the order of the parameters and see the results.
One more thing, when I first wrote the intersect method, it probably looked like this:
Copy the code The code is as follows:

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 very many elements in the array, the memory occupied will be relatively large. On the contrary, if there are not very many elements in the array, then this method is also feasible. (Source: Huo Ding’s Notes)

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/323093.htmlTechArticleHowever, because mobile phones have many parameters and the parameters of different mobile phones vary greatly, the parameter table structure is usually a vertical table. (One parameter is one row), rather than a horizontal table (one parameter is one column),...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn