Looks to me like the result is real. Adding a few more test cases is interesting:

use strict; use warnings; use Benchmark qw(cmpthese); my $pre = 250; my $post = 250; my $baz_match = ('b' x $pre) . 'baz' . ('b' x $post); my $bar_match = ('b' x $pre) . 'bar' . ('b' x $post); my $no_match = ('b' x $pre) . 'bza' . ('b' x $post); print "using_or_index ", using_or_index (), "\n"; print "using_or_noindex ", using_or_noindex (), "\n"; print "using_or_match ", using_or_match (), "\n"; print "using_or_nomatch ", using_or_nomatch (), "\n"; print "using_alt_match ", using_alt_match (), "\n"; print "using_alt_nomatch ", using_alt_nomatch (), "\n"; print "using_set_match ", using_set_match (), "\n"; print "using_set_nomatch ", using_set_nomatch (), "\n"; cmpthese( -1, { using_or_index => \&using_or_index, using_or_noindex => \&using_or_noindex, using_or_match => \&using_or_match, using_or_nomatch => \&using_or_nomatch, using_alt_match => \&using_alt_match, using_alt_nomatch => \&using_alt_nomatch, using_set_match => \&using_set_match, using_set_nomatch => \&using_set_nomatch, }); sub using_or_index { (-1 != index $baz_match, 'bar') or (-1 != index $baz_match, 'baz') + ? 1 : 0 } sub using_or_noindex { (-1 != index $no_match, 'bar') or (-1 != index $no_match, 'baz') ? + 1 : 0 } sub using_or_match { ($baz_match =~ /bar/ or $baz_match =~ /baz/) ? 1 : 0 } sub using_or_nomatch { ($no_match =~ /bar/ or $no_match =~ /baz/) ? 1 : 0 } sub using_alt_match { ($baz_match =~ /bar|baz/) ? 1 : 0 } sub using_alt_nomatch { ($no_match =~ /bar|baz/) ? 1 : 0 } sub using_set_match { ($baz_match =~ /ba[rz]/) ? 1 : 0 } sub using_set_nomatch { ($no_match =~ /ba[rz]/) ? 1 : 0 }

Prints:

using_or_index 1 using_or_noindex 0 using_or_match 1 using_or_nomatch 0 using_alt_match 1 using_alt_nomatch 0 using_set_match 1 using_set_nomatch 0 Rate using_alt_nomatch using_alt_match using_or_ +nomatch using_or_noindex using_set_nomatch using_or_match using_or_in +dex using_set_match using_alt_nomatch 9692/s -- -22% + -99% -99% -99% -99% - +99% -99% using_alt_match 12385/s 28% -- + -98% -98% -98% -98% - +98% -99% using_or_nomatch 667281/s 6785% 5288% + -- -4% -5% -17% - +19% -26% using_or_noindex 696846/s 7090% 5527% + 4% -- -1% -13% - +16% -22% using_set_nomatch 701884/s 7142% 5567% + 5% 1% -- -13% - +15% -22% using_or_match 804828/s 8204% 6399% + 21% 15% 15% -- +-2% -10% using_or_index 825420/s 8417% 6565% + 24% 18% 18% 3% + -- -8% using_set_match 898573/s 9172% 7155% + 35% 29% 28% 12% + 9% --

My guess is that the regex $str =~ /foo/ devolves to something pretty much like Index $str, 'foo' which can be pretty quick!

The surprise is how fast using a character set with a common prefix match is. That result also goes a long way to validating the benchmark.


DWIM is Perl's answer to Gödel

In reply to Re: Benchmarking regex alternation by GrandFather
in thread Benchmarking regex alternation by Tanktalus

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.