Edit report at http://bugs.php.net/bug.php?id=53887&edit=1

 ID:                 53887
 Updated by:         ahar...@php.net
 Reported by:        paulgao at yeah dot net
 Summary:            When array data is int value, array_intersect()
                     speed VERY SLOW.
-Status:             Open
+Status:             Bogus
 Type:               Bug
 Package:            Arrays related
 Operating System:   irrelevant
 PHP Version:        Irrelevant
 Block user comment: N
 Private report:     N

 New Comment:

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.


Previous Comments:
------------------------------------------------------------------------
[2011-01-31 08:19:57] 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


------------------------------------------------------------------------



-- 
Edit this bug report at http://bugs.php.net/bug.php?id=53887&edit=1

Reply via email to