With this version of use_str:

sub use_str { my $re = qr/^1\d2\d3\d4\d5\d6\d7\d8\d900$/; for (my $n = 1_010_101_030; $n <= 1_389_026_623; ) { # stringify the qr regex my $str = "$re"; my $s = $n * $n; return $n if $s =~ /$str/; # recompiled each time I guess? $n += 40; $s = $n * $n; return $n if $s =~ /$str/; $n += 60; } die; }
I get:
Benchmark: timing 5 iterations of use_qr, use_re, use_str... use_qr: 33 wallclock secs (33.62 usr + 0.00 sys = 33.62 CPU) @ 0 +.15/s (n=5) use_re: 18 wallclock secs (17.14 usr + 0.00 sys = 17.14 CPU) @ 0 +.29/s (n=5) use_str: 25 wallclock secs (24.99 usr + 0.00 sys = 24.99 CPU) @ 0 +.20/s (n=5) s/iter use_qr use_str use_re use_qr 6.72 -- -26% -49% use_str 5.00 35% -- -31% use_re 3.43 96% 46% -- Appuyez sur une touche pour continuer...
So this looks like perl is doing even worse with qr than it would by recompiling the stringified version on each iteration (unless I'm missing some optimization, but I tried adding dummy logic like rand(100) < 100 to prevent perl from noticing that the expression are constants, and didn't see significant changes)?

Edit: perl v5.20 here BTW


In reply to Re: Performance penalty of using qr// by Eily
in thread Performance penalty of using qr// by Athanasius

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.