Optimizing code Analysis of the intersection of PHP arrays _php tips

Source: Internet
Author: User
Tags rand
But because of the number of phone parameters, and different mobile phone parameters vary greatly, 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 the individual parameters to take the results, and then intersection together.
Suppose that each parameter contains 1000 or so unique results (ID int), which is the precondition for simulating the generation of some data:
Copy Code code as follows:

<?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: If the test data set is too small, the conclusion may be inconsistent, let's take a look at the performance of the PHP built-in method Array_intersect implementation:
Copy Code code as follows:

<?php
$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 of the Intersect implementation through custom methods:
Copy Code code as follows:

<?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 certainly think that a built-in function is faster than a custom function, but in this case the result is exactly the opposite:
array_intersect:0.023918151855469
intersect:0.0026049613952637
We need to remind you that Array_intersect and intersect are not completely functionally equivalent, as the following examples are:
Copy Code code 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 is a duplicate element in the first array parameter, 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 to see the result.
Again, when I first wrote the Intersect method, I probably write the following:
Copy Code code as follows:

<?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 more concise, but there is a drawback, because the use of array_merge, so when the elements of the array is very large, the memory will be larger, conversely, if the elements of the array is not very much, then this method is feasible. (Source: Fire Ding Notes)

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.