php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #53887 When array data is int value, array_intersect() speed VERY SLOW.
Submitted: 2011-01-31 08:19 UTC Modified: 2011-02-01 05:30 UTC
From: paulgao at yeah dot net Assigned:
Status: Not a bug Package: Arrays related
PHP Version: Irrelevant OS: irrelevant
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: paulgao at yeah dot net
New email:
PHP Version: OS:

 

 [2011-01-31 08:19 UTC] paulgao at yeah dot net
Description:
------------
When array data is int value, array_intersect() speed VERY SLOW.

Test script:
---------------
<?php

ini_set('memory_limit', -1);

$data_a = rand_data(FALSE);
$data_b = rand_data(FALSE);

$time = microtime(TRUE);

$result = array_intersect($data_a, $data_b);

$time = microtime(TRUE) - $time;

echo " -> array_intersect by intval: {$time}, result: " . count($result) . "\r\n";

$data_a = rand_data(TRUE);
$data_b = rand_data(TRUE);

$time = microtime(TRUE);

$result = array_intersect($data_a, $data_b);

$time = microtime(TRUE) - $time;

echo " -> array_intersect by string: {$time}, result: " . count($result) . "\r\n";

function rand_data($need_string)
{
    mt_srand();

    $result = array();

    for ($i = 0; $i < 300000; $i++)
    {
        if ($need_string === TRUE)
        {
            $result[] = (string)mt_rand();
        }
        else
        {
            $result[] = mt_rand();
        }
    }

    return $result;
}

?>

Expected result:
----------------
almost same?

Actual result:
--------------
 -> array_intersect by intval: 10.661009073257, result: 47
 -> array_intersect by string: 1.3067090511322, result: 41

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-02-01 05:30 UTC] aharvey@php.net
-Status: Open +Status: Bogus
 [2011-02-01 05:30 UTC] aharvey@php.net
As the documentation says, array_intersect() internally compares the values of the arrays as strings. This implies that non-string values (such as integers) need to be converted to their equivalent string values first before being compared, which obviously takes some time.

I suspect we'd be interested in a patch that improved the performance if it maintained the existing comparison behaviour, but for now, I'm closing this as it is documented.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue May 07 04:01:30 2024 UTC