Try it without the () in your first regex. The RE engine has to do a bit of setup work every time it enters or leaves a capturing () set during its passage through the candidate string, so they add a lot of overhead.

Also, your "||" version isn't very fair, since the /est/ always matches, meaning the /\d/ test is never made. But it is interesting how much faster the "constant" match for est is as opposed to the match for est|\d.

Oh, and use Benchmark:

#!/usr/bin/perl -w use strict; use Benchmark qw(cmpthese); my $i=0; sub capture { "test$i"=~/(est|\d)/; } sub noCapture { "test$i"=~/(?:est|\d)/; } sub noParens { "test$i"=~/est|\d/; } sub twoRegexen { "test$i"=~/est/ || "test$i"=~/\d/; } cmpthese(-3, { capture => \&capture, noCapture => \&noCapture, noParens => \&noParens, twoRegexen => \&twoRegexen, } );
Results (with 5.6.1, I'm sure 5.8.0 would be somewhat different):
$ perl testRegexen.pl Benchmark: running capture, noCapture, noParens, twoRegexen, each for +at least 3 CPU seconds... capture: 4 wallclock secs ( 3.18 usr + 0.00 sys = 3.18 CPU) @ 72 +8822.24/s (n=2320570) noCapture: 2 wallclock secs ( 3.10 usr + 0.01 sys = 3.11 CPU) @ 82 +7692.26/s (n=2576606) noParens: 4 wallclock secs ( 3.14 usr + 0.01 sys = 3.15 CPU) @ 87 +1936.55/s (n=2748344) twoRegexen: 4 wallclock secs ( 3.41 usr + -0.00 sys = 3.41 CPU) @ 14 +78048.90/s (n=5047537) Rate capture noCapture noParens twoRegexen capture 728822/s -- -12% -16% -51% noCapture 827692/s 14% -- -5% -44% noParens 871937/s 20% 5% -- -41% twoRegexen 1478049/s 103% 79% 70% --

--
Mike

Edit: Oops, when I added the noParens case I forgot to put it into the cmpthese call here, although I'd put it into my test script.


In reply to Re: RE performance by RMGir
in thread RE performance by Vennis

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.