2012-03-28 74 views
-1

我需要您的幫助。假設我在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

回答

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通話將數據添加到您的文件。

+0

我用過這個。除了我必須使用不同的文件句柄打印每個文件句柄,然後再將它們連接起來。但它的工作。謝謝! – Fighter2 2012-03-30 01:09:09

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 
+0

WOw。我從來沒有聽說過這個模塊。讓我試着去學習它。這將明確地對我稍後有用。 – Fighter2 2012-03-30 01:09:48