use Benchmark qw( cmpthese timethese );
my @alphabet = ( 0 .. 9, 'a' .. 'z', 'A' .. 'Z' );
my $h = horrid_rx(1_000);
my $qr_h = qr/$h/;
my $qr_ho = qr/$h/o;
my $s = join q{}, map { $alphabet[ rand @alphabet ] } 1 .. 1_000;
my $matchiness = ( $s =~ /$h/ ) ? 'matches' : 'does not match';
print "horrid rx $matchiness string\n";
my $loops = 1_000;
cmpthese( -2, { '//' => sub { $s =~ /$h/ for (1..$loops) },
'/o' => sub { $s =~ /$h/o for (1..$loops) },
'qr' => sub { $s =~ $qr_h for (1..$loops) },
'qr/o' => sub { $s =~ $qr_ho for (1..$loops) },
} );
sub horrid_rx {
my ($n) = @_;
my @quant = ( '*', '?', '+', '{0,1}', );
my $out;
for ( 1 .. $n ) {
$out .= $alphabet[ rand @alphabet ];
$out .= $quant[ rand @quant ];
}
return $out;
}
####
horrid rx does not match string
Rate // qr/o qr /o
// 129/s -- -83% -83% -88%
qr/o 752/s 483% -- -0% -27%
qr 753/s 483% 0% -- -27%
/o 1037/s 704% 38% 38% --
####
horrid rx does not match string
Rate // qr qr/o /o
// 44.4/s -- -29% -29% -32%
qr 62.7/s 41% -- -1% -4%
qr/o 63.0/s 42% 1% -- -4%
/o 65.4/s 47% 4% 4% --