Using a /(foo|bar)/ regex on strings is slower than using a foreach loop doing the matching one after another.
Not in my benchmark:
use strict; use warnings; use Benchmark 'cmpthese'; undef $/; my $str = <DATA>; cmpthese(-2, { alternation => sub { my @m = $str =~ /(foo|bar)/g }, map => sub { my @m = map $str =~ /$_/g, qw(foo bar) }, loop => sub { my @m; push @m, $str =~ /$_/g for qw(foo bar) } +, }); cmpthese(-2, { alt_s => sub { my $s2 = $str; $s2 =~ s/(foo|bar)//g }, loop_s => sub { my $s2 = $str; $s2 =~ s/$_//g for qw(foo bar) } }); __DATA__ This string contains foo and bar for fools and bards and I pity the foo who bars the way
Rate map loop alternation map 14977/s -- -6% -25% loop 15986/s 7% -- -20% alternation 20090/s 34% 26% -- Rate loop_s alt_s loop_s 17436/s -- -37% alt_s 27643/s 59% --
Update:
But I converted your example program to use Benchmark, and I do get better results with for:
use strict; use Benchmark 'cmpthese'; foreach my $regexcount (10) { foreach my $regexlength (5,20) { my @items = map{ createRandomTextWithLength($regexlength) } + (1..$regexcount); my $regexstr = join('|',@items); my $regex = qr/(?:$regexstr)/; foreach my $stringlength (1000,100000) { print join "\n", "Stringlength: $stringlength", "Number of Regexes:$regexcount", "Length of each Regex +:$regexlength\n"; my $teststring = createRandomTextWithLength($stringlength) +; cmpthese(-2, { alt => sub { my $test=$teststring; $test =~ s/$regex/foobar/g; }, for => sub { my $test=$teststring; foreach my $oneregex (@items) { $test =~ s/$oneregex/foobar/g; } } }); } } } sub createRandomTextWithLength($) { my($count) = (@_); my $string; for (1.. $count) { $string.=chr(ord('a')+rand(20)) } return $string; }
Results:
Stringlength: 1000 Number of Regexes:10 Length of each Regex:5 Rate alt for alt 1296/s -- -70% for 4357/s 236% -- Stringlength: 100000 Number of Regexes:10 Length of each Regex:5 Rate alt for alt 12.7/s -- -97% for 379/s 2877% -- Stringlength: 1000 Number of Regexes:10 Length of each Regex:20 Rate alt for alt 1324/s -- -68% for 4144/s 213% -- Stringlength: 100000 Number of Regexes:10 Length of each Regex:20 Rate alt for alt 12.7/s -- -98% for 676/s 5209% --

Caution: Contents may have been coded under pressure.

In reply to Re: Regex combining /(foo|bar)/ slower than using foreach (/foo/,/bar/) ??? by Roy Johnson
in thread Regex combining /(foo|bar)/ slower than using foreach (/foo/,/bar/) ???   by JollyJinx

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.