use strict; use warnings; use feature 'say'; use re 'debug'; my $str = 'foobar'; sub foo { map /$str/, 1 .. 3 } foo for 1 .. 2; #### Compiling REx "foobar" Final program: 1: EXACT (4) 4: END (0) anchored "foobar" at 0 (checking anchored isall) minlen 6 Compiling REx "foobar" Compiling REx "foobar" Compiling REx "foobar" Compiling REx "foobar" Compiling REx "foobar" #### use strict; use warnings; use Benchmark qw( cmpthese timethese ); open my $words, "<", "linuxwords.txt" or die "$!"; my @words = <$words>; chomp @words; my @search = @words[0..10]; $" = "|"; my $re = qr/^(?:@words)$/; my $str = "^(?:@words)\$"; my $str1 = $str; my $r = timethese ( -5, { use_qr => sub { map /$re/, @search }, use_str => sub { substr $str1, rand 400_000, 1, '#'; # 'equalize' conditions with 'use_str1' map /$str/, @search }, use_str1 => sub { substr $str1, rand 400_000, 1, '#'; # force proper re-compilation below map /$str1/, @search }, } ); cmpthese $r; #### Rate use_str1 use_str use_qr use_str1 3.65/s -- -99% -100% use_str 436/s 11853% -- -99% use_qr 31592/s 865014% 7138% --