有什么方法可以优化以下脚本以更快地运行?
foreach my $arg (@data){ #
@score=();
`program $arg $arg1 > $result`; #!!! $arg1 is a very large file with lots of data!!!
open(FH,$result);
while(<FH>){
chomp;
if($_ =~ /\d+.+\s+(\d+\.\d+|\d+\.|\.\d+).+/){ #here i'm looking for any number such as: 21.343 or 12 or 0.22 or -3.0
push(@score, $1);
}
}
close FH;
@sorted = sort{$a <=> $b} @score; #a sorted score is what i actually want
}
Run Code Online (Sandbox Code Playgroud)
我可以看到一些东西(例如没有立即将结果加载到文件中),但我怀疑你将获得的主要性能优势可能来自使用不同的正则表达式.为此,您是否更好地了解程序中的数据输出格式是什么?
这里有一些可能运行得更快的perl示例:
use strict;
foreach my $arg (@data){
my @score=();
open(my $fh, "program $arg $arg1 |");
while (<$fh>) {
chomp;
if (/\d+.+\s+((\d+)?\.?\d+)/o) {
push(@score, $1);
}
}
close($fh);
my @sorted = sort { $a <=> $b } @score;
}
Run Code Online (Sandbox Code Playgroud)
请注意以下几点:
其他人都说要使用线程.你不需要这样做,因为像在open函数中使用尾随管道(|)一样运行进程会导致perl为你分叉一个进程.然后使用标准的unix管道异步读取程序.
| 归档时间: |
|
| 查看次数: |
284 次 |
| 最近记录: |