gpt4 book ai didi

text - 使用Perl6处理大文本文件,太慢了。(2014-09)

转载 作者:行者123 更新时间:2023-12-04 19:05:38 24 4
gpt4 key购买 nike

代码宿主在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 代码要好得多。 :)

附注。整个事情,包括原始测试代码、补丁和示例文本,都在 github 上。

最佳答案

我使用包含 10,000 行的文件使用与 Christoph 非常相似的代码对此进行了测试。它需要大约 15 秒,正如你所说,比 Perl 5 慢得多。我怀疑代码很慢,因为这段代码使用的东西没有像 Rakudo 和 MoarVM 的其他部分最近收到的那样多的优化工作。我确信代码的性能将在接下来的几个月内得到显着提高,因为任何慢的都会引起更多关注。

在尝试确定某些 Perl 6 代码为什么很慢时,我建议在 MoarVM 上使用 --profile 运行 perl6 以查看它是否可以帮助您找到瓶颈。不幸的是,使用此代码,它将指向 rakudo 内部结构,而不是您可以改进的任何内容。

在 irc.freenode.net 上与 #perl6 交谈当然值得,因为他们将拥有提供替代解决方案的知识,并且能够在 future 提高其性能。

关于text - 使用Perl6处理大文本文件,太慢了。(2014-09),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25638528/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com