Regex speed in Perl 6

落爺英雄遲暮 提交于 2019-11-29 14:23:56

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 ~@array.grep($search);

That change causes it to run in about half a second.

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

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)

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!