Regular Expression Filtering and .contains in Perl 6

I often have to filter line items arraythat contain some substring (for example, one character). Since this can be done either by matching the method regex, or with the method .contains, I decided to do a little test to make sure it is .containsfaster (and therefore more appropriate).

my @array = "aa" .. "cc";
my constant $substr = 'a';

my $time1 = now;
my @a_array = @array.grep: *.contains($substr);
my $time2 = now;
@a_array = @array.grep: * ~~ /$substr/;
my $time3 = now;

my $time_contains = $time2 - $time1;
my $time_regex    = $time3 - $time2;
say "contains: $time_contains sec";
say "regex:    $time_regex sec";

Then I resize @arrayand length $substrand compare the time that each method took for filtering @array. In most cases (as expected) .containsmuch faster than regex, especially if @arraylarge. But in the case of a small one @array(as in the above code) a regexlittle faster.

contains: 0.0015010 sec
regex:    0.0008708 sec

Why is this happening?

+4
1

, , , "regex vs contains", " ":

:

contains: 0.001555  sec
regex:    0.0009051 sec

:

regex:    0.002055 sec
contains: 0.000326 sec

- . - , .

, script , , script, (, multi sub MAIN("task1") ). , .

IRC- # perl6 freenode benchable6, . " " wiki, , .

+4

Source: https://habr.com/ru/post/1688515/


All Articles