开发者

Comparing large numbers of values using php arrays

I have to compare two very large number of values, for that I put them in arrays but it didn't work. Below is the code I use. Is this the most efficient way? I have set the time and memory to unlimited as well. error 101 (connection reset) unknown error this is error shown by chrome

for ($k = 0; $k < sizeof($pid); $k++) {
    $out = 0;
    for ($m = 0; $m < sizeof($oid); $m++) {
        if ($pid[$k] == $oi开发者_如何学运维d[$m]) // $pid have 300000 indexes
        //and $oid have about 500000 indexes               
        {
            $out++;
        }

    }
    if ($out) {
        echo "OID for ID ".$pid[$k]." = ".$out;
        echo "<br>";
    } 
}


Doesn't work how? Won't give you an answer?
You're comparing every possible pair. How many combinations is that? More than 10^13. That will take something like an hour on a modern machine, if you don't run out of memory first.

A more efficient way would be to sort them first: NlogN + MlogM + N + M time, instead of N*M time.
Sorting a list of size x with a comparison sort takes x*log(x) time. Then, you can walk from the front of each list once, confident that if there are any matches you will find them. This takes linear time.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜