hmm.. yup. after much fiddling with the tests, I couldn't get qr to match /o. My conclusion is:
if you've got a regex containing a variable that will not change, use /o.
if you need to loop through multiple regexes, then you can't use /o, so compile them with qr.
the option to that is to have multiple tests using /o which I think would still be faster.. ok off to test..

again, hmmm..

use Benchmark; my @words = map { chomp; $_ } (<DATA>); my $alpha = '[a-zA-Z]'; my $alnum = '[a-zA-Z0-9]'; my @qr = ( qr/^$alpha/, qr/$alnum+$/ ); timethese(500000, { 'With /o' => \&testsub, 'qr' => \&testsubqr, }); sub testsub{ my $count = 0; foreach(@words){ $count += testsubb($_); } } sub testsubb { my $word = $_[0]; return unless $word =~ /^$alpha/o; return unless $word =~ /$alnum+$/o; return 1; } sub testsubqr{ my $count = 0; foreach(@words){ $count += testsubbqr($_); } } sub testsubbqr { my $word = $_[0]; foreach(@qr){ return unless $word =~ $_; } return 1; } Benchmark: timing 500000 iterations of With /o, qr... With /o: 12 wallclock secs (13.13 usr + 0.01 sys = 13.14 CPU) @ 38 +051.75/s (n=500000) qr: 18 wallclock secs (18.90 usr + 0.01 sys = 18.91 CPU) @ 26 +441.04/s (n=500000) Benchmark: timing 100000 iterations of With /o, qr... With /o: 3 wallclock secs ( 2.64 usr + 0.00 sys = 2.64 CPU) @ 37 +878.79/s (n=100000) qr: 4 wallclock secs ( 3.78 usr + 0.00 sys = 3.78 CPU) @ 26 +455.03/s (n=100000)

I think I'm done defending qr..

ok, back to work.. nothing to see here...

update: adding these tests shows that the loop is adding more time than qr saves, but /o is still quicker.. so where does that leave us?

sub testsubqr2{ my $count = 0; foreach(@words){ $count += testsubbqr2($_); } } sub testsubbqr2 { my $word = $_[0]; return unless $word =~ $qr[0]; return unless $word =~ $qr[1]; return 1; } Benchmark: timing 100000 iterations of With /o, qr, qr2... With /o: 3 wallclock secs ( 2.63 usr + 0.00 sys = 2.63 CPU) @ 38 +022.81/s (n=100000) qr: 4 wallclock secs ( 3.76 usr + 0.00 sys = 3.76 CPU) @ 26 +595.74/s (n=100000) qr2: 3 wallclock secs ( 2.98 usr + 0.00 sys = 2.98 CPU) @ 33 +557.05/s (n=100000)

cheers,

J


In reply to Re: Re: Re: Never by edoc
in thread Never-to-use Perl features? by Juerd

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.