in reply to Re: Case in regular expressions
in thread Case in regular expressions
In the second edition of his book, Friedl drily announces that readers of the first edition need not worry any longer about the /i modifier, since the issue has been already fixed.
Here is a test, showing that the difference, if any, is rather small. Getting less than 0.20 sec difference with ten thousand iterations on a one-million-char string, I would choose the /i modifier any time.
It all depends on your version of Perl and your machine speed, but if you have a recent release of both, you can safely use the /i modifier without losing much sleep.
#!/usr/bin/perl -w use strict; use Benchmark qw(timethese); for my $size (10_000, 100_000, 1_000_000) { my $string = 'a' x $size . ' While '; print "string size $size\n", ; my $x; timethese(10_000, { i_modifier => sub { $x = 1 if $string =~ m/\bwhile\b/i; }, char_class => sub { $x = 1 if $string =~ m/\b[Ww][Hh][Ii][Ll][Ee]\b/; } }); } __END__ Perl 5.6.1 ========== string size 10000 Benchmark: timing 10000 iterations of char_class, i_modifier... char_class: 1 wallclock secs ( 0.62 usr + 0.00 sys = 0.62 CPU) i_modifier: 1 wallclock secs ( 0.62 usr + 0.00 sys = 0.62 CPU) string size 100000 Benchmark: timing 10000 iterations of char_class, i_modifier... char_class: 6 wallclock secs ( 6.05 usr + 0.01 sys = 6.06 CPU) i_modifier: 6 wallclock secs ( 6.05 usr + 0.01 sys = 6.06 CPU) string size 1000000 Benchmark: timing 10000 iterations of char_class, i_modifier... char_class: 64 wallclock secs (62.17 usr + 0.31 sys = 62.48 CPU) i_modifier: 63 wallclock secs (61.87 usr + 0.25 sys = 62.12 CPU) ActiveState Perl 5.8.0 (optimized for Pentium architecture) =========================================================== string size 10000 Benchmark: timing 10000 iterations of char_class, i_modifier... char_class: 1 wallclock secs ( 0.51 usr + 0.00 sys = 0.51 CPU) i_modifier: 1 wallclock secs ( 0.52 usr + 0.00 sys = 0.52 CPU) string size 100000 Benchmark: timing 10000 iterations of char_class, i_modifier... char_class: 5 wallclock secs ( 5.12 usr + 0.02 sys = 5.14 CPU) i_modifier: 5 wallclock secs ( 5.23 usr + 0.02 sys = 5.25 CPU) string size 1000000 Benchmark: timing 10000 iterations of char_class, i_modifier... char_class: 51 wallclock secs (50.74 usr + 0.15 sys = 50.89 CPU) i_modifier: 51 wallclock secs (50.91 usr + 0.15 sys = 51.06 CPU)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Re: Case in regular expressions
by davido (Cardinal) on Sep 13, 2003 at 16:31 UTC |