in reply to Why this code run faster?

#!/usr/bin/perl # use Benchmark qw( cmpthese ); cmpthese( -2, { g => sub { 'network' =~ /^network$/g }, c => sub { 'network' =~ /^network$/gc }, s => sub { 'networK' =~ /^networK$/ }, } ); __END__ Rate s g c s 2106781/s -- -28% -68% g 2931188/s 39% -- -56% c 6602248/s 213% 125% --

The /c modifier really boosts! Why? It doesn't reset the search position on a failed match while /g is in effect (see perlop). So it tests once from the beginning, and at each further invocation of the same match, it tests beginning at the end, failing quickly. With a single /g, every other match fails.

--shmem

_($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                              /\_¯/(q    /
----------------------------  \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}