UPD: If I pass the search tokens to grep, looping through the @search array, the program works much faster:my @array = "aaaaa" .. "fffff";say +@array;my @search = <abcde cdeff fabcd>;for @search -> $token { say [email protected]({/$token/});}$ time perl6 search.p6real 0m1,378suser 0m1,400ssys 0m0,052s如果我手动定义每个搜索模式,它的工作速度会更快:And if I define each search pattern manually, it works even faster:my @array = "aaaaa" .. "fffff";say +@array; # 7776 = 6 ** 5say [email protected]({/abcde/});say [email protected]({/cdeff/});say [email protected]({/fabcd/});$ time perl6 search.p6real 0m0,587suser 0m0,632ssys 0m0,036s推荐答案grep 命令比 Perl 6 的正则表达式简单得多,而且它还有很多年的时间来优化.这也是 Rakudo 中没有看到太多优化的领域之一;部分原因是它被认为是一件很难处理的事情.The grep command is much simpler than Perl 6's regular expressions, and it has had many more years to get optimized. It is also one of the areas that hasn't seen as much optimizing in Rakudo; partly because it is seen as being a difficult thing to work on.对于性能更高的示例,您可以预编译正则表达式:For a more performant example, you could pre-compile the regex:my $search = "/@search.join('|')/".EVAL;# $search = /abcde|cdeff|fabcd/;say [email protected]($search);该更改使其在大约半秒内运行.That change causes it to run in about half a second.如果 @search 中存在恶意数据的可能性,并且您必须这样做,那么使用可能更安全:If there is any chance of malicious data in @search, and you have to do this it may be safer to use:"/@search».Str».perl.join('|')/".EVAL编译器不能完全为 /@search/ 生成优化的代码,因为 @search 在编译正则表达式后可能会改变.可能发生的情况是,第一次使用正则表达式时,它会被重新编译为更好的形式,然后只要 @search 没有被修改就将其缓存.(我认为 Perl 5 做了类似的事情)The compiler can't quite generate that optimized code for /@search/ as @search could change after the regex gets compiled. What could happen is that the first time the regex is used it gets re-compiled into the better form, and then cache it as long as @search doesn't get modified.(I think Perl 5 does something similar)您必须牢记的一个重要事实是,Perl 6 中的正则表达式只是一种用特定领域的子语言编写的方法.One important fact you have to keep in mind is that a regex in Perl 6 is just a method that is written in a domain specific sub-language. 这篇关于Perl 6 中的正则表达式速度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-21 05:17