我注意到我的 Ruby(1.9)脚本存在极度的延迟问题,经过一些挖掘,发现问题在于正则表达式的匹配。我使用了以下 Perl 和 Ruby 的测试脚本:
Perl:
$fname = shift(@ARGV);
open(FILE, "<$fname" );
while (<FILE>) {
if ( /(.*?) \|.*?SENDING REQUEST.*?TID=(.*?),/ ) {
print "$1: $2\n";
}
}
Ruby:
=>Ruby:
f = File.open( ARGV.shift )
while ( line = f.gets )
if /(.*?) \|.*?SENDING REQUEST.*?TID=(.*?),/.match(line)
puts "#{$1}: #{$2}"
end
end
我在两个脚本中使用相同的输入,一个只有44290行的文件。 每个脚本的时间如下:
Perl:
xenofon@cpm:~/bin/local/project$ time ./try.pl input >/dev/null
real 0m0.049s
user 0m0.040s
sys 0m0.000s
Ruby:
xenofon@cpm:~/bin/local/project$ time ./try.rb input >/dev/null
real 1m5.106s
user 1m4.910s
sys 0m0.010s
我想我在做某些非常愚蠢的事情,有什么建议吗?
谢谢
if line =~ /(.*?) \|.*?SENDING REQUEST.*?TID=(.*?),/
吗?这在 Ruby 中也可以工作,我很想知道它是否具有不同的性能特征。 - Michael Kohl