Match Anything, Quickly -- Revision 1

O wad some Power the giftie gie us
To see oursels as ithers see us!
It wad frae mony a blunder free us,
An' foolish notion: ...

My previous blog post, Match Anything, Quickly, brought a number of responses which are worth reading in their own right. The one that triggered this post, though, was from Nerdvana and Devin of Cincinnati Perl Mongers, who pointed out an error in my benchmark script. I had left off the intended /smx from the qr/ ... / version of the test, which meant that the regular expression did not in fact match.

Three cheers for code reviews!

The Cincinnati Perl Mongers came up with a further case which combines my two:

eval "do { my \$regex = qr/ $re /smx; " .
        "sub { \$MATCH =~ /\$regex/o }};"

They benchmarked this as being slightly slower than the case where the regular expression is simply interpolated into the subroutine verbatim.

Interestingly (to me, at least) they reported that the removal of the /o modifier made their case 2-3 times slower. This surprised me somewhat, as I had understood that modern Perls (for some value of "modern") had done things to minimize the performance difference between the presence and absence of /o.

For the record, the corrected script is also on GitHub. The corrections include an option that tests to make sure all benchmarked things actually match. The result of running this with the --test and --html options (on a different machine than the original post) is:

ok 1 - sub { 1 }
ok 2 - sub { $MATCH =~ m/ (*ACCEPT) /smx }
ok 3 - qr/ (*ACCEPT) /smx
ok 4 - sub { $MATCH =~ m/ (?) /smx }
ok 5 - qr/ (?) /smx
ok 6 - sub { $MATCH =~ m/ (?:) /smx }
ok 7 - qr/ (?:) /smx
ok 8 - sub { $MATCH =~ m/ .? /smx }
ok 9 - qr/ .? /smx
ok 10 - sub { $MATCH =~ m/ .{0} /smx }
ok 11 - qr/ .{0} /smx
ok 12 - sub { $MATCH =~ m/ \A /smx }
ok 13 - qr/ \A /smx
ok 14 - sub { $MATCH =~ m/ ^ /smx }
ok 15 - qr/ ^ /smx
1..15
ImplementationRate
sub { 1 }434782608.70/sec
sub { $MATCH =~ m/ \A /smx }13333333.33/sec
sub { $MATCH =~ m/ ^ /smx }13315579.23/sec
sub { $MATCH =~ m/ (?:) /smx }12315270.94/sec
sub { $MATCH =~ m/ (?) /smx }11173184.36/sec
sub { $MATCH =~ m/ .{0} /smx }10593220.34/sec
sub { $MATCH =~ m/ .? /smx }10449320.79/sec
sub { $MATCH =~ m/ (*ACCEPT) /smx }4380201.49/sec
qr/ ^ /smx2612330.20/sec
qr/ \A /smx2603488.67/sec
qr/ (?:) /smx2586652.87/sec
qr/ (?) /smx2575991.76/sec
qr/ .{0} /smx2518891.69/sec
qr/ .? /smx2510670.35/sec
qr/ (*ACCEPT) /smx1849796.52/sec

Leave a comment

About Tom Wyant

user-pic I blog about Perl.