https://github.com/yeahnoob/perl6-perf中的代码宿主,如下:
use v6;
my $file=open "wordpairs.txt", :r;
my %dict;
my $line;
repeat {
$line=$file.get;
my ($p1,$p2)=$line.split(' ');
if ?%dict{$p1} {
%dict{$p1} = "{%dict{$p1}} {$p2}".words;
} else {
%dict{$p1} = $p2;
}
} while !$file.eof;
当“wordpairs.txt”很小时运行良好。
但是当“wordpairs.txt”文件大约有 140,000 行(每行,两个单词)时,它运行非常非常慢。即使运行了 20 秒,它也无法完成自身。
它有什么问题?代码有问题吗??感谢任何人的帮助!
以下内容已添加@ 2014-09-04,感谢 SE Answers 和 IRC@freenode#perl6 的许多建议
代码(现在,2014-09-04):
my %dict;
grammar WordPairs {
token word-pair { (\S*) ' ' (\S*) "\n" }
token TOP { <word-pair>* }
}
class WordPairsActions {
method word-pair($/) { %dict{$0}.push($1) }
}
my $match = WordPairs.parse(slurp, :actions(WordPairsActions));
say ?$match;
运行时间成本(目前):
$ time perl6 countpairs.pl wordpairs.txt
True
The pairs count of the key word "her" in wordpairs.txt is 1036
real 0m24.043s
user 0m23.854s
sys 0m0.181s
$ perl6 --version
This is perl6 version 2014.08 built on MoarVM version 2014.08
这个测试的时间性能目前不合理(因为相同的正确 Perl 5 代码只花费大约 160 毫秒),但比我原来的旧 Perl6 代码要好得多。:)
PS。整个东西,包括原始测试代码、补丁和示例文本,都在 github 上。