I benchmarked this and it yields an interesting result. index() is (a bit) faster than a regex. If itīs used in combination with lc(), as in your example, the regex with the i-modifier is faster.
use strict;
use warnings;
use Benchmark;
my $value = "somewhere here true is there!";
timethese
(
9000000,
{
'index' => sub { index( $value, "true" ) },
'regex' => sub { $value =~ /true/ },
}
);
timethese
(
9000000,
{
'index' => sub { index( lc $value, "true" ) },
'regex' => sub { $value =~ /true/i },
}
);
Benchmark: timing 9000000 iterations of index, regex...
index: 2 wallclock secs ( 2.02 usr + 0.00 sys = 2.02 CPU) @ 44
+46640.32/s (n=9000000)
regex: 4 wallclock secs ( 2.40 usr + -0.01 sys = 2.39 CPU) @ 37
+60969.49/s (n=9000000)
Benchmark: timing 9000000 iterations of index, regex...
index: 4 wallclock secs ( 4.55 usr + 0.00 sys = 4.55 CPU) @ 19
+79762.43/s (n=9000000)
regex: 3 wallclock secs ( 3.68 usr + 0.00 sys = 3.68 CPU) @ 24
+48313.38/s (n=9000000)
Update:
Ack. I really need to learn to type faster.
-
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.
|