PHP:没有array_count_values获得结果的更好方法,因为它太慢了

时间:2017-10-16 19:23:30

标签: php arrays performance large-files

我的脚本合并了18个文件,并返回合并中发生的> = 13次的所有数字。我计时我的脚本和array_count_values是如此之慢,它占2.35秒时间的80%。文件很大,每个文件有200,000个数字,因此合并后的数组远远超过200万。

我有什么想法可以踢出array_count_values函数或者以更好的方式编写它,并且仍然可以返回合并数组中出现> = 13次的所有数字?

注意:我缩短了代码,仅反映了18个合并中的3个文件。

for($b=0; $b<1; $b++)
{
    echo $b."\n";
for($a=0; $a<10; $a++)
{

    for($i=0; $i<30; $i++)//30
{
    $linespreset=file_get_contents("/users/history/".$folder."/".$round."/masterspeedrandom_randompick_less13_".$b."_".$a."_".$i.".txt");

    $holdpreset=explode(" ",$linespreset);
    $holdpreset=array_map("trim", $holdpreset);
$print1=file_get_contents('/users/'.$a.'/masterspeed_round3_xxx_'.$holdpreset[0].'.txt');
$print2=file_get_contents('/users/'.$a.'/masterspeed_round3_xxx_'.$holdpreset[1].'.txt');
$print3=file_get_contents('/users/'.$a.'/masterspeed_round3_xxx_'.$holdpreset[2].'.txt');

$healthy = " ";
$yummy   = "_";
$print1= strtr($print1,$healthy,$yummy);
$print2= strtr($print2,$healthy,$yummy);
$print3= strtr($print3,$healthy,$yummy);

$resultround=$print1."\r\n".$print2."\r\n".$print3."\r\n".$print4."\r\n".$print5."\r\n".$print6."\r\n".$print7."\r\n".$print8."\r\n".$print9."\r\n".$print10."\r\n".$print11."\r\n".$print12."\r\n". $print13."\r\n".$print14."\r\n".$print15."\r\n".$print16."\r\n".$print17."\r\n".$print18;

$somearray = str_word_count($resultround, 1, '1234567890:@&_');

$frequency = array_count_values($somearray);

$result = array_filter($frequency, function ($x) { return $x >=13; });

unset($somearray);

}//END OF I
}//END OF A

}//END OF B

1 个答案:

答案 0 :(得分:0)

我认为foreach速度更快,但我没有测试过它

foreach ($somearray as $somearrayelement) {
    $frequency[$somearrayelementkey]++;
}