我需要帮助来剖析我的代码

问题描述:

我需要您的帮助。假设我在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}"); 
+1

显示完整的代码。 - [append访问模式](http://p3rl.org/opentut)拼写为'>>'。 – daxim 2012-03-28 09:49:28

+0

用较小的输入数据尝试一下,看看这些子程序在做什么。 – 2012-03-28 19:07:57

最简单的解决方法是加印抛入循环,并呼吁他们每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通话将数据添加到您的文件。

+0

我用过这个。除了我必须使用不同的文件句柄打印每个文件句柄,然后再将它们连接起来。但它的工作。谢谢! – Fighter2 2012-03-30 01:09:09

使用备受好评的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 
+0

WOw。我从来没有听说过这个模块。让我试着去学习它。这将明确地对我稍后有用。 – Fighter2 2012-03-30 01:09:48