优化数组合并操作 [英] Optimizing array merge operation

查看:88
本文介绍了优化数组合并操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果有帮助,我将不胜感激。

I would appreciate any help, given.

我有7个独立的数组,大约每个数组中有90,000个数字(我们称它们为arrays1-arrays7)。每个数组本身内没有重复的数字。但是,阵列之间可能有重复项。例如,array2没有重复项,但是可能有与arrays3和arrays4相同的数字。

I have 7 separate arrays with approx. 90,000 numbers in each array (let's call them arrays1-arrays7). There are no duplicate numbers within each array itself. BUT, there can be duplicates between the arrays. for example, array2 has no duplicates but it is possible to have numbers in common with arrays3 and arrays4.

问题:
我正在尝试识别所有所有7个数组合并后重复3次的数字。

The Problem: I am trying to identify all of the numbers that are duplicated 3 times once all 7 arrays are merged.

我必须进行1000次此计算,并且需要15分钟,但这并不可行,因为我必须运行40次-代码:

I must do this calculation 1000 times and it takes 15 mins but that is not ok because I have to run it 40 times -- The code:

如果您知道另一种最适合此类计算的语言,请告诉我。

if you know of another language that is best suited for this type of calculation please let me know. any extension suggestions such as 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分钟:

i have also tried the code below with strings but the array_map call and the array_count values call take 17 mins:

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);


推荐答案

array_merge()在数组中包含更多元素的情况下明显较慢,因为(来自 php.net ):

array_merge() is significantly slower with more elements in the array because (from php.net):


如果输入数组具有相同的字符串键,则后面的值
用于该密钥将覆盖前一个密钥。但是,如果数组
包含数字键,则后面的值将不会覆盖原始的
值,而是将其附加。

If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended.

使用数字键的输入数组将使用
递增键从结果数组中的零开始重新编号。

Values in the input array with numeric keys will be renumbered with incrementing keys starting from zero in the result array.

因此此函数实际上是在做一些条件语句。您可以用普通的添加替换数组合并,它由循环(foreach或任何其他)和 [] 运算符组成。您可以编写一个模仿array_merge的函数,例如(使用引用而不复制数组。):

So this function is actually making some conditional statements. You can replace array merge with normal adding, consisting of the loop (foreach or any other) and the [] operator. You can write a function imitating array_merge, like(using reference to not copy the array..):

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

您会发现速度确实很难提高。

And you will see the increase of speed really hard.

这篇关于优化数组合并操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆