Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2024-04-24 13:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found