我需要你的帮助。假设我在for语句中有一个类似于下面所示的代码。
$ logCount的价值很大。像一百万。在我的循环中有一些哈希值会在一段时间内增长,它会运行我的系统内存!我应该做的是,通过循环然后将结果写入文件。但由于我的内存不足,这种情况永远不会发生。因此,我想将我的循环分解为1000步。
你能帮忙吗?有更聪明的方法吗?如果我打破循环,我不知道如何附加到文件的底部。for (my $i=0; $i < $logCount; $i++){
# Crap code
# Herp Derp
generatePowerRecord($sectorMapper->{'sectors'}, \$powerresult, ,\%Dimensions, \@powers, \@Attn, $sectors, $count-$totalcount);
generatePhaseRecord($sectorMapper->{'sectors'}, \$phaseresult, ,\%Dimensions);
generateDelayRecord($sectorMapper->{'sectors'}, \$delayresult, ,\%Dimensions, \@delay_history, \$sectors, $count-$totalcount);
};
$fh->print($dataresult);
$fh->print($powerresult);
$fh->print($phaseresult);
$fh->print($delayresult);
$fh->print("\n}");
答案 0 :(得分:3)
使用备受推崇的Devel::NYTProf分析模块。
从概要:
# profile code and write database to ./nytprof.out
perl -d:NYTProf some_perl.pl
# convert database into a set of html files, e.g., ./nytprof/index.html
# and open a web browser on the nytprof/index.html file
nytprofhtml --open
# or into comma separated files, e.g., ./nytprof/*.csv
nytprofcsv
答案 1 :(得分:0)
最简单的解决方案是在循环中抛出额外的打印件,并在每1000次迭代中调用它们,如下所示:
for (my $i=0; $i < $logCount; $i++){
# Crap code
# Herp Derp
generatePowerRecord($sectorMapper->{'sectors'}, \$powerresult, ,\%Dimensions, \@powers, \@Attn, $sectors, $count-$totalcount);
generatePhaseRecord($sectorMapper->{'sectors'}, \$phaseresult, ,\%Dimensions);
generateDelayRecord($sectorMapper->{'sectors'}, \$delayresult, ,\%Dimensions, \@delay_history, \$sectors, $count-$totalcount);
# call this every 1000th iteration
if($i > 0 and $i % 1000 == 0) {
$fh->print($dataresult);
$fh->print($powerresult);
$fh->print($phaseresult);
$fh->print($delayresult);
# cleanup hashes
undef $dataresult;
undef $powerresult;
undef $phaseresult;
undef $delayresult;
}
};
print
调用会将数据附加到您的文件中,直到它保持打开状态。