Dear monks,

I've hit a performance issue with using | in regexes. It seems that in some (not-so-degenerated, actually) cases it loses significantly to looping over simple regexes i.e. $str =~ /$_// for @rx is much faster than $str =~ /$rx[0]|$rx[1]|$rx[2]/, which is rather counter-intuitive. Basically, for general cases, it would mean that alterations with grouping should be avoided at all, which is a strong statement and I wouldn't like it that way.

Is this a recognized problem? Is it a problem at all? Does it look like it needs to be reported as a bug? I can't decide myself.

Here's the test code:

use strict; use warnings; use Benchmark qw(:all); my $str = 'a' x 100; my @matchwords = qw( aol aachen aaliyah aaron abbas abbasid abbott abby abdul abe abel abel +ard abelson aberdeen abernathy abidjan abigail abilene abner abraham abram abrams +absalom abuja abyssinia abyssinian ac acadia acapulco accra acevedo achaean ); my $q1s = join('|', map { "$_\\s*\\w+" } @matchwords ); my $q1 = qr/$q1s/; my @q2s = map { "$_\\s*\\w+" } @matchwords; my @q2 = map { qr/$_/ } @q2s; my $q3s = join('|', map { "($_)\\s*\\w+" } @matchwords ); my $q3 = qr/$q3s/; my @q4s = map { "($_)\\s*\\w+" } @matchwords; my @q4 = map { qr/$_/ } @q4s; timethese( 100000, { 'alternation, no grouping' => sub { $str =~ /$q1/; }, 'loop, no grouping' => sub { for my $qr ( @q2 ) { $str =~ /$qr/; } }, 'alternation, grouping' => sub { $str =~ /$q3/; }, 'loop, grouping' => sub { for my $qr ( @q4 ) { $str =~ /$qr/; } }, });

Here's the output:

Benchmark: timing 100000 iterations ... alternation, grouping: 12 wallclock secs (11.92 usr + 0.00 sys = 11.9 +2 CPU) @ 8389.26/s (n=100000) alternation, no grouping: 0 wallclock secs ( 0.19 usr + 0.00 sys = +0.19 CPU) @ 526315.79/s (n=100000) (warning: too few iterations for a reliable count) loop, grouping: 2 wallclock secs ( 1.33 usr + 0.00 sys = 1.33 CPU) +@ 75187.97/s (n=100000) loop, no grouping: 1 wallclock secs ( 1.33 usr + 0.00 sys = 1.33 CP +U) @ 75187.97/s (n=100000)

Update: got same results on perls 5.10.1, 5.16.0, and 5.17.6


In reply to alternation in regexes: to use or to avoid? by dk

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.