# a: if( \$s =~ m[(lazy)] ){ \$found=\$1 } Compiling REx "(lazy)" Final program: 1: OPEN1 (3) 3: EXACT (5) 5: CLOSE1 (7) 7: END (0) anchored "lazy" at 0 (checking anchored) minlen 4 Matching REx "(lazy)" against "the quick brown fox jumps over the lazy dog" Intuit: trying to determine minimum start position... Found anchored substr "lazy" at offset 35... (multiline anchor test skipped) try at offset... Intuit: Successfully guessed: match at offset 35 35 < the > | 1:OPEN1(3) 35 < the > | 3:EXACT (5) 39 < dog> | 5:CLOSE1(7) 39 < dog> | 7:END(0) Match successful! Freeing REx: "(lazy)" # b: \$found = 'lazy' if 1+index( \$s, 'lazy' ); # c: if( \$s =~ m[lazy] ){ \$found=\$& } Compiling REx "lazy" Final program: 1: EXACT (3) 3: END (0) anchored "lazy" at 0 (checking anchored isall) minlen 4 Matching REx "lazy" against "the quick brown fox jumps over the lazy dog" Intuit: trying to determine minimum start position... Found anchored substr "lazy" at offset 35... (multiline anchor test skipped) try at offset... Intuit: Successfully guessed: match at offset 35 Freeing REx: "lazy" Rate a c b a 2038631/s -- -50% -75% c 4089154/s 101% -- -49% b 8013601/s 293% 96% -- ##```## use strict; use Benchmark 'cmpthese'; use vars '\$s'; \$s='the quick brown fox jumps over the lazy dog'; my \$found; my %benchmarks = ( a => q[ if( \$s =~ m[(lazy)] ){ \$found=\$1 } ], b => q[ \$found = 'lazy' if 1+index( \$s, 'lazy' ); ], c => q[ if( \$s =~ m[lazy] ){ \$found=\$& } ], ); { use re 'debug'; for (sort keys %benchmarks) { print "# \$_: \$benchmarks{\$_}\n"; undef \$found; my \$code = eval qq{sub { \$benchmarks{\$_} } } or die "Couldn't compile benchmark \$_: \$@"; \$code->(); \$found eq 'lazy' or die "Unexpected results: [\$found] vs. 'lazy'"; }; }; cmpthese( -1, \%benchmarks); ```