Array Merge Operation Optimization

I would be grateful for any help provided.

I have 7 separate arrays with approx. 90,000 numbers in each array (let's call them arrays1-arrays7). There are no duplicate numbers in each array. BUT, there may be duplicates between arrays. for example, array 2 has no duplicates, but you can have common numbers with arrays3 and arrays4.

Problem: I am trying to identify all numbers that are duplicated 3 times when all 7 arrays are combined.

I have to do this calculation 1000 times, and it takes 15 minutes, but this is not normal, because I have to run it 40 times - Code:

if you know another language that is best suited for this type of computation, please let me know. any additional suggestions like redis or gearman are helpful.

for($kj=1; $kj<=1000; $kj++)
    {
$result=array_merge($files_array1,$files_array2,$files_array3,$files_array4,$files_array5,$files_array6,$files_array7);

$result=array_count_values($result);

$fp_lines = fopen("equalTo3.txt", "w");

foreach($result as $key => $val)
{
    if($result[$key]==3)
    {
    fwrite($fp_lines, $key."\r\n");
    }
}
fclose($fp_lines);
}

, array_map array_count 17 :

for($kj=1; $kj<=1000; $kj++)
    {

$result='';

for ($ii = 0; $ii< 7; $ii++) {
    $result .= $files_array[$hello_won[$ii]].'\r\n';
}

$result2=explode("\n",$result);//5mins
$result2=array_map("trim",$result2);//11mins
$result2=array_count_values($result2);//4-6mins

$fp_lines = fopen("equalTo3.txt", "w");

foreach($result2 as $key => $val)
{

    if($result2[$key]==3)
    {
    fwrite($fp_lines, $key."\r\n");
    }
}
fclose($fp_lines);

unset($result2);

///////////////////////////////////////////////////

@ziumin @ailvenge @scunliffe @this.lau_ @monocell , ! , . !

+4
3

array_merge() , ( php.net):

, . , , , , .

, .

, . , (foreach ) []. , array_merge, ( , ..):

function imitateMerge(&$array1, &$array2) {
    foreach($array2 as $i) {
        $array1[] = $i;
    }
}

.

+5

, , ,

, array_merge, , , . , . , , , overlap (rand(0, 200000), , ), .

function arrValues($arrs) {
    $values = array();

    foreach($arrs as $arr) {
        foreach($arr as $key => $val) {
            if(array_key_exists($key, $values)) {
                $values[$val]++;
            } else {
                $values[$val] = 1;
            }
        }
    }
    return $values;
}

var_dump(arrValues(array
    ($files_array1
    ,$files_array2
    ,$files_array3
    ,$files_array4
    ,$files_array5
    ,$files_array6
    ,$files_array7
    )));

0,5 , 2s .

-edit -

, 1000 ? - ? ...

-edit -

, array_merge . , , , . ? 7 90k ~ 250M. php , , , , , , , ? , php ? 5.5 5.4 , 0,5 . . , 1000 PHP , . , .

, , . , , ~ 500M . php. .

+1

foreach($result as $key => $val)
{
    if($result[$key]==3)
    {
    fwrite($fp_lines, $key."\r\n");
    }
}

-

$res = array_keys(array_filter($result, function($val){return $val == 3;}));
fwrite($fp_lines, implode("\r\n", $res));
0
source

Source: https://habr.com/ru/post/1538459/


All Articles