我做了下面的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