Optimization of the intersection of PHP arrays

Source: Internet
Author: User
Tags foreach array arrays continue count rand sort

Let's say we're running a mobile-related web site, and users can filter the phones they want by specifying a number of parameters (such as operating system, screen resolution, camera pixels, and so on). 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 about 1000 product IDs (int), which is the precondition for simulating the generation of some data:

<?php

$rand = function () {
    $result = array ();

    for ($i = 0; $i < 1000 $i + +) {
        $result [] = Mt_rand (1, 10000);
    }

    return $result;
};

$param _a = $rand ();
$param _b = $rand ();

? >

Note: If the test data set is too small, the conclusion may appear inconsistent.

Let's take a look at the performance of Array_intersect through PHP's built-in methods:

<?php

$time = Microtime (true);

$result = Array_intersect ($param _a, $param _b);

$time = Microtime (True)-$time;

echo "Array_intersect: {$time}\n";

? >

Before we optimize, let's take a look at some of the Array_intersect special places:

<?php

$param _a = Array (1, 2, 2);
$param _b = Array (1, 2, 3);

Var_dump (
    array_intersect ($param _a, $param _b),
    array_intersect ($param _b, $param _a)
);

>
    • Array_intersect ($param _a, $param _b): 1, 2, 2
    • Array_intersect ($param _b, $param _a): 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. These features are best compatible when rewriting array_intersect.

Here's a look at the performance of implementing Int_array_intersect through a custom method:

<?php function Int_array_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) {continue;
            } if ($a [$i] > $b [$j] && + + $j) {continue;

            $result [] = $a [$i];
            if (isset ($a [$next = $i + 1]) && $a [$next]!= $a [$i]) {+ + $j;
        + + $i;
    return $result;

    };
    $result = Array_shift ($args);

    Sort ($result); foreach ($args as $arg) {sORT ($ARG);
    $result = $intersect ($result, $arg);
return $result;

} $time = Microtime (true);

$result = Int_array_intersect ($param _a, $param _b);

$time = Microtime (True)-$time;

echo "Int_array_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
    • int_array_intersect:0.0026049613952637

Why? The reason is that the int_array_intersect operations are all integers, and the Array_intersect action is a string, and if you pass it to an integer, it does a step-time "(string)" type conversion operation.

Note: Test results are based on PHP5.3.5 and different versions of conclusions may differ




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.