Optimized code analysis for the intersection of PHP Arrays _php Tutorial

Source: Internet
Author: User
However, because of the number of parameters of the phone, and the different parameters of the phone, so the parameter table structure is usually a vertical table (a parameter is a row), rather than a horizontal table (a parameter is a column), at this time using a number of parameters to take the results, usually is to take each individual parameter to the result, and then take the intersection
Assuming that each parameter contains 1000 or so unique results (id int), this is the premise to simulate generating some data:
Copy CodeThe code is as follows:
$rand = function () {
$result = Array ();
for ($i = 0; $i <; 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 conclusion may be inconsistent, first take a look at the performance implemented by PHP's built-in method Array_intersect:
Copy CodeThe code is as follows:
$time = Microtime (true);
$result = Array_intersect ($param _a, $param _b);
$time = Microtime (True)-$time;
echo "Array_intersect: {$time}\n";
?>

Take a look at the performance achieved by customizing the method intersect:
Copy CodeThe 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 certainly think that built-in functions are faster than custom functions, but in this case the results are exactly the opposite:
array_intersect:0.023918151855469
intersect:0.0026049613952637
It is necessary to remind you that array_intersect and intersect are functionally not entirely equivalent, as in the following example:
Copy CodeThe 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
That is, if there are repeating elements in the first array argument, then Array_intersect returns all the repeating elements that satisfy the condition, instead of just returning one, interested readers can change the order of the parameters and see the results.
Again, when I first wrote the Intersect method, I would probably write the following:
Copy CodeThe 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 disadvantage, because the use of array_merge, so when the array of elements very much, the memory will be larger, and conversely if the array of elements is not very much, then this method is also feasible. (Source: Fire Ding Notes)

http://www.bkjia.com/PHPjc/323093.html www.bkjia.com true http://www.bkjia.com/PHPjc/323093.html techarticle However, because of the number of mobile phone parameters, and different phone parameters of the large differences, so the parameter table structure is usually a vertical table (a parameter is a row), not a horizontal table (a parameter is a column), ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.