Regex speed in Perl 6

后端 未结 1 851
抹茶落季
抹茶落季 2020-12-20 23:00

I\'ve been previously working only with bash regular expressions, grep, sed, awk etc. After trying Perl 6

相关标签:
1条回答
  • 2020-12-20 23:12

    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.

    0 讨论(0)
提交回复
热议问题