You're right, publishing a benchmark without the test data is pretty meaningless. Here's a revised version that uses the individual words output from 'perldoc -t perlfunc' as the test data.

#!/usr/local/bin/perl -w use Benchmark; my (@words, $count); open(TESTDATA, "perldoc -t perlfunc|") || die $!; while(<TESTDATA>) { chomp; push @words, /(\S+)/g } print @words . " words\n"; my $alpha = '[a-zA-Z]'; my $alnum = '[a-zA-Z0-9]'; my $qr = qr/^$alpha$alnum+$/; timethese(100, { '/^$alpha$alnum+$/ ' => \&testsub, '/^$alpha$alnum+$/o' => \&testsubo, '/$qr/ ' => \&testsubqr1, '$qr ' => \&testsubqr2, '/$qr/o ' => \&testsubqro, }); sub testsub { foreach (@words) { $count++ if(/^$alpha$alnum+$/); } + } sub testsubo { foreach (@words) { $count++ if(/^$alpha$alnum+$/o); } + } sub testsubqr1 { foreach (@words) { $count++ if(/$qr/); } + } sub testsubqr2 { foreach (@words) { $count++ if($_ =~ $qr); } + } sub testsubqro { foreach (@words) { $count++ if(/$qr/o); } + }

This is probably a fairer test than the original (less iterations of more data) and the output looks like this:

/^$alpha$alnum+$/ : 20 wallclock secs (20.41 usr + 0.00 sys = 20.41 C +PU) @ 4.90/s (n=100) /^$alpha$alnum+$/o: 9 wallclock secs ( 8.34 usr + 0.00 sys = 8.34 C +PU) @ 11.99/s (n=100) /$qr/ : 9 wallclock secs ( 9.59 usr + 0.00 sys = 9.59 C +PU) @ 10.43/s (n=100) $qr : 10 wallclock secs ( 9.94 usr + 0.00 sys = 9.94 C +PU) @ 10.06/s (n=100) /$qr/o : 9 wallclock secs ( 8.34 usr + 0.01 sys = 8.35 C +PU) @ 11.98/s (n=100)

The reason I used /$qr/ rather than =~ $qr was not because I didn't know how to use it, but because I was using it in an if statement and $qr being a reference would simply evaluate to true without even attempting a match. The results above appear to show that plain $qr is slightly slower than /$qr/ but that is almost certainly due to the fact that I had to spell it out as $_ =~ $qr and so the difference should be disregarded.


In reply to Re: Re^2: Never (qr//) by grantm
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.