G'day Athanasius,
I decided to look at this from a slightly different angle.
Instead of involving complex routines and matches,
I picked a regex match that was so simple that an 'eq' comparison would be, in normal code, a better choice:
'X' =~ /^X$/
I ran this benchmark, using that match as a base, but incorporating a whole series of variations:
with/without 'qr//', the 'o' modifier, and many types of variables.
#!/usr/bin/env perl
use 5.010;
use strict;
use warnings;
use Benchmark 'cmpthese';
use constant STRING => 'X';
use constant CONST_RE => qr{^X$};
my $my_re = qr{^X$};
state $state_re = qr{^X$};
our $our_re = qr{^X$};
local $main::local_re = qr{^X$};
cmpthese 0 => {
re_str => sub { STRING =~ '^X$' },
re_re => sub { STRING =~ /^X$/ },
re_re_o => sub { STRING =~ /^X$/o },
re_qr => sub { STRING =~ qr{^X$} },
re_qr_o => sub { STRING =~ qr{^X$}o },
qr_my => sub { STRING =~ $my_re },
qr_my_re => sub { STRING =~ /$my_re/ },
qr_my_re_o => sub { STRING =~ /$my_re/o },
qr_state => sub { STRING =~ $state_re },
qr_state_re => sub { STRING =~ /$state_re/ },
qr_state_re_o => sub { STRING =~ /$state_re/o },
qr_our => sub { STRING =~ $our_re },
qr_our_re => sub { STRING =~ /$our_re/ },
qr_our_re_o => sub { STRING =~ /$our_re/o },
qr_const => sub { STRING =~ CONST_RE },
qr_const_re => sub { STRING =~ /${\CONST_RE()}/ },
qr_const_re_o => sub { STRING =~ /${\CONST_RE()}/o },
qr_local => sub { STRING =~ $main::local_re },
qr_local_re => sub { STRING =~ /$main::local_re/ },
qr_local_re_o => sub { STRING =~ /$main::local_re/o },
};
[I was aware that "qr_const_re" and "qr_const_re_o" might produce bogus results
due to the additional reference and dereference operations;
however, I left them in purely out of curiosity.]
Here's the results just showing the rates.
(The complete results are in a spoiler at the end of my post.)
Rate
re_qr 550709/s
re_qr_o 560597/s
qr_local 1061718/s
qr_const_re 1065053/s
qr_state 1065891/s
qr_local_re 1077507/s
qr_our 1089135/s
qr_state_re 1089138/s
qr_my_re 1092539/s
qr_my 1096982/s
qr_our_re 1101420/s
qr_state_re_o 4073421/s
qr_local_re_o 4085064/s
qr_my_re_o 4279146/s
qr_const_re_o 4293130/s
qr_our_re_o 4302931/s
re_re_o 4647519/s
qr_const 4745450/s
re_re 4748039/s
re_str 4814042/s
Some of those numbers are too close to call with respect to what was faster than what;
however, this general trend appears to emerge (fastest to slowest):
-
'^X$', /^X$/ and, when created at compile time, qr{^X$}.
-
All that used the 'o' modifier (except for qr{^X$}o).
-
Those using variables assigned with qr{^X$}; either as $var or /$var/.
-
Clearly the slowest of all: qr{^X$} and qr{^X$}o (created at runtime).
I ran that a few times.
The specific order changed a bit but the general trend I've indicated seemed to hold.
In light of ++dave_the_m's input, I'd like to see how captures might affect those results.
I don't have time to do this myself now: perhaps you, or someone else, would care to tinker.
The full results are in the spoiler, below.
I needed to stretch the console window to 220 characters to avoid wrapping.
Rate re_qr re_qr_o qr_local qr_const_re qr_state qr
+_local_re qr_our qr_state_re qr_my_re qr_my qr_our_re qr_state_re_o q
+r_local_re_o qr_my_re_o qr_const_re_o qr_our_re_o re_re_o qr_const re
+_re re_str
re_qr 550709/s -- -2% -48% -48% -48%
+ -49% -49% -49% -50% -50% -50% -86%
+ -87% -87% -87% -87% -88% -88% -
+88% -89%
re_qr_o 560597/s 2% -- -47% -47% -47%
+ -48% -49% -49% -49% -49% -49% -86%
+ -86% -87% -87% -87% -88% -88% -
+88% -88%
qr_local 1061718/s 93% 89% -- -0% -0%
+ -1% -3% -3% -3% -3% -4% -74%
+ -74% -75% -75% -75% -77% -78% -
+78% -78%
qr_const_re 1065053/s 93% 90% 0% -- -0%
+ -1% -2% -2% -3% -3% -3% -74%
+ -74% -75% -75% -75% -77% -78% -
+78% -78%
qr_state 1065891/s 94% 90% 0% 0% --
+ -1% -2% -2% -2% -3% -3% -74%
+ -74% -75% -75% -75% -77% -78% -
+78% -78%
qr_local_re 1077507/s 96% 92% 1% 1% 1%
+ -- -1% -1% -1% -2% -2% -74%
+ -74% -75% -75% -75% -77% -77% -
+77% -78%
qr_our 1089135/s 98% 94% 3% 2% 2%
+ 1% -- -0% -0% -1% -1% -73%
+ -73% -75% -75% -75% -77% -77% -
+77% -77%
qr_state_re 1089138/s 98% 94% 3% 2% 2%
+ 1% 0% -- -0% -1% -1% -73%
+ -73% -75% -75% -75% -77% -77% -
+77% -77%
qr_my_re 1092539/s 98% 95% 3% 3% 2%
+ 1% 0% 0% -- -0% -1% -73%
+ -73% -74% -75% -75% -76% -77% -
+77% -77%
qr_my 1096982/s 99% 96% 3% 3% 3%
+ 2% 1% 1% 0% -- -0% -73%
+ -73% -74% -74% -75% -76% -77% -
+77% -77%
qr_our_re 1101420/s 100% 96% 4% 3% 3%
+ 2% 1% 1% 1% 0% -- -73%
+ -73% -74% -74% -74% -76% -77% -
+77% -77%
qr_state_re_o 4073421/s 640% 627% 284% 282% 282%
+ 278% 274% 274% 273% 271% 270% --
+ -0% -5% -5% -5% -12% -14% -
+14% -15%
qr_local_re_o 4085064/s 642% 629% 285% 284% 283%
+ 279% 275% 275% 274% 272% 271% 0%
+ -- -5% -5% -5% -12% -14% -
+14% -15%
qr_my_re_o 4279146/s 677% 663% 303% 302% 301%
+ 297% 293% 293% 292% 290% 289% 5%
+ 5% -- -0% -1% -8% -10% -
+10% -11%
qr_const_re_o 4293130/s 680% 666% 304% 303% 303%
+ 298% 294% 294% 293% 291% 290% 5%
+ 5% 0% -- -0% -8% -10% -
+10% -11%
qr_our_re_o 4302931/s 681% 668% 305% 304% 304%
+ 299% 295% 295% 294% 292% 291% 6%
+ 5% 1% 0% -- -7% -9%
+-9% -11%
re_re_o 4647519/s 744% 729% 338% 336% 336%
+ 331% 327% 327% 325% 324% 322% 14%
+ 14% 9% 8% 8% -- -2%
+-2% -3%
qr_const 4745450/s 762% 746% 347% 346% 345%
+ 340% 336% 336% 334% 333% 331% 16%
+ 16% 11% 11% 10% 2% --
+-0% -1%
re_re 4748039/s 762% 747% 347% 346% 345%
+ 341% 336% 336% 335% 333% 331% 17%
+ 16% 11% 11% 10% 2% 0%
+ -- -1%
re_str 4814042/s 774% 759% 353% 352% 352%
+ 347% 342% 342% 341% 339% 337% 18%
+ 18% 13% 12% 12% 4% 1%
+ 1% --