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
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
22 - 21 = ?
Subscribe to this entry?

 
 [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: Fri Mar 29 00:01:28 2024 UTC