I think that for the index case the situation is not so clear. Both the regex engine and index() will use the same Boyer-Moore routine and for me personally, the regex version is more readable. But as always, YMMV.
use Benchmark qw(:all) ; my $value = 'FALSE'; my $count = 1_000_000; cmpthese($count, { 'regex' => sub { $value =~ /^true$/i }, 'eq' => sub { lc $value eq "true" }, 'index' => sub { index( lc $value, "true" ) >= 0 }, });
yields
Benchmark: timing 1000000 iterations of eq, index, regex... eq: 1 wallclock secs ( 0.89 usr + 0.00 sys = 0.89 CPU) @ 11 +23595.51/s (n=1000000) index: 2 wallclock secs ( 1.65 usr + 0.00 sys = 1.65 CPU) @ 60 +6060.61/s (n=1000000) regex: 2 wallclock secs ( 1.63 usr + 0.00 sys = 1.63 CPU) @ 61 +3496.93/s (n=1000000) Rate index regex eq index 606061/s -- -1% -46% regex 613497/s 1% -- -45% eq 1123596/s 85% 83% --
Update: As AM has pointed out (thank you!), the benchmark above has a bug. Using the tests
'regex' => sub { $value =~ /true/i }, 'regex_anch' => sub { $value =~ /^true$/i }, 'eq' => sub { lc $value eq "true" }, 'index' => sub { index( lc $value, "true" ) >= 0 },
I get the results
Benchmark: timing 1000000 iterations of eq, index, regex, regex_anch.. +. eq: 1 wallclock secs ( 0.88 usr + 0.00 sys = 0.88 CPU) @ 11 +36363.64/s (n=1000000) index: 0 wallclock secs ( 1.65 usr + 0.00 sys = 1.65 CPU) @ 60 +6060.61/s (n=1000000) regex: 0 wallclock secs ( 1.08 usr + 0.00 sys = 1.08 CPU) @ 92 +5925.93/s (n=1000000) regex_anch: 2 wallclock secs ( 1.59 usr + 0.00 sys = 1.59 CPU) @ 62 +8930.82/s (n=1000000) Rate index regex_anch regex eq index 606061/s -- -4% -35% -47% regex_anch 628931/s 4% -- -32% -45% regex 925926/s 53% 47% -- -19% eq 1136364/s 87% 81% 23% --
with the surprising result that the regex w/o the anchor is faster than the anchored version. Multiple runs yield similar results. As the AM says, one could try many different regex-value combos, but I expect the results to be not far different, precisely because both index and regex engine use the same BM function.

-Mark


In reply to Re^2: You don't always have to use regexes by kvale
in thread You don't always have to use regexes by petdance

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.