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).
array
regex
.contains
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.
@array
$substr
contains: 0.0015010 sec regex: 0.0008708 sec
Why is this happening?
, , , "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") ). , .
multi sub MAIN("task1")
IRC- # perl6 freenode benchable6, . " " wiki, , .
Source: https://habr.com/ru/post/1688515/More articles:MvvmCross 5.4 Crash при запуске приложения с помощью NullRef на ConsoleLogProvider - linkerInternationalization of YAML - phpsort an array that has a hyphen in javascript - javascriptThe application cannot work on devices running Android 7.1 after switching to Android Studio 3 - androidAlternative row colors angular material table - htmlAngular Material 2 Table Mat Row Click an event also triggered by a button click in Mat Cell - materialList of directories added or deleted between two git commits - gitAutomation WDIO. Specification characters are skipped in the given text after updating the Chrome browser - google-chromeЭмулятор: эмулятор: ПРЕДУПРЕЖДЕНИЕ: UpdateCheck: Failure: Error - androidHow can I identify files / directories that have been added or removed in a git declaration? - gitAll Articles