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)
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.