我做了下面的Perl腳本來處理一些文件操作,但是它在運行時的運行速度太慢了。優化Perl腳本 - 在40GB +文件上運行速度太慢
我不太熟悉Perl(不是我的一種語言),所以有人可以幫助我識別和替換這個腳本的部分內容,因爲它處理了大約4000萬行,所以它會很慢。
數據在被管道輸送的格式爲:
col1|^|col2|^|col3|!|
col1|^|col2|^|col3|!|
... 40 million of these.
的date_cols陣列腳本的這一部分之前計算並基本上保持包含在轉換前的格式的日期的列的索引。
以下是將爲每個輸入行執行的腳本部分。我已經清理了一點,添加評論,但讓我知道是否需要任何東西:
## Read from STDIN until no more lines are arailable.
while (<STDIN>)
{
## Split by field delimiter
my @fields = split('\|\^\|', $_, -1);
## Remove the terminating delimiter from the final field so it doesn't
## interfere with date processing.
$fields[-1] = (split('\|!\|', $fields[-1], -1))[0];
## Cycle through all column numbres in date_cols and convert date
## to yyyymmdd
foreach $col (@date_cols)
{
if ($fields[$col] ne "")
{
$fields[$col] = formatTime($fields[$col]);
}
}
print(join('This is an unprintable ASCII control code', @fields), "\n");
}
## Format the input time to yyyymmdd from 'Dec 26 2012 12:00AM' like format.
sub formatTime($)
{
my $col = shift;
if (substr($col, 4, 1) eq " ") {
substr($col, 4, 1) = "0";
}
return substr($col, 7, 4).$months{substr($col, 0, 3)}.substr($col, 4, 2);
}
你有沒有想過首先使用'csplit'之類的東西將文件拆分成碎片? – matchew
這是如何工作的,並且假設我在所有的部分上運行這個腳本後它能夠重新組裝它們嗎? –
我沒有看到任何明顯的低效率。 'print'函數是迄今爲止顯示速度最慢的,但我認爲這只是爲了調試目的。如果你正好運行這個代碼(減去'print'),它仍然很慢? *我有點懷疑,因爲'trim'子沒有在任何地方使用。* – dan1111