I've been previously working only with bash
regular expressions, grep
, sed
, awk
etc. After trying Perl 6
regexes
I've got an impression that they work slower than I would expect, but probably the reason is that I handle them incorrectly.
I've made a simple test to compare similar operations in Perl 6
and in bash
. Here is the Perl 6
code:
my @array = "aaaaa" .. "fffff";
say +@array; # 7776 = 6 ** 5
my @search = <abcde cdeff fabcd>;
my token search {
@search
}
my @new_array = @array.grep({/ <search> /});
say @new_array;
Then I printed @array
into a file named array
(with 7776 lines), made a file named search
with 3 lines (abcde
, cdeff
, fabcd
) and made a simple grep
search.
$ grep -f search array
After both programs produced the same result, as expected, I measured the time they were working.
$ time perl6 search.p6
real 0m6,683s
user 0m6,724s
sys 0m0,044s
$ time grep -f search array
real 0m0,009s
user 0m0,008s
sys 0m0,000s
So, what am I doing wrong in my Perl 6 code?
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.p6
real 0m1,378s
user 0m1,400s
sys 0m0,052s
And if I define each search pattern manually, it works even faster:
my @array = "aaaaa" .. "fffff";
say +@array; # 7776 = 6 ** 5
say [email protected]({/abcde/});
say [email protected]({/cdeff/});
say [email protected]({/fabcd/});
$ time perl6 search.p6
real 0m0,587s
user 0m0,632s
sys 0m0,036s
See Question&Answers more detail:
os