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.
-
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.