in reply to Slowness of /i

It's not as bad as generating all combinations. Roughly speaking, the regex engine only needs to compare upper- and lower-case versions of the next character in the pattern, and it gets to move on as soon as it finds a match.

As you suggest, if speed matters it's best to apply lc to the searched text and use a lower-cased regex pattern.

After Compline,
Zaxo

Replies are listed 'Best First'.
Re^2: Slowness of /i
by tlm (Prior) on May 01, 2005 at 19:13 UTC

    Actually, pre-applying lc did not fare so well:

    use strict; use warnings; use Benchmark 'cmpthese'; my $string = 'TwAs BrIlLiG aNd ThE sLiThY tOvEs DiD gYrE aNd GiMbLe'; cmpthese( -1, { i_s => sub { local $_ = $string; /gimble/i }, I_s => sub { local $_ = $string; /GiMbLe/ }, lc_s => sub { local $_ = lc $string; /gimble/ }, i_f => sub { local $_ = $string; /foobar/i }, I_f => sub { local $_ = $string; /foobar/ }, lc_f => sub { local $_ = lc $string; /foobar/ }, } ); __END__ Rate lc_f lc_s i_s I_s i_f I_f lc_f 459364/s -- -4% -13% -27% -33% -33% lc_s 477204/s 4% -- -10% -24% -30% -30% i_s 530962/s 16% 11% -- -15% -22% -22% I_s 628278/s 37% 32% 18% -- -8% -8% i_f 681314/s 48% 43% 28% 8% -- -0% I_f 681315/s 48% 43% 28% 8% 0% --

    the lowliest monk