Created
May 28, 2015 11:19
-
-
Save reyjrar/54c69ed1c7817368fa46 to your computer and use it in GitHub Desktop.
Regex Modifier: o
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
# | |
use strict; | |
use warnings; | |
use Benchmark qw(cmpthese timethese); | |
my @tests = ( | |
q{testing 12345 some more stuff}, | |
q{123 testing some stuff}, | |
q{this is a really long sentence which is intentionally not giving you a number, so don't even try, it's not going to happen.}, | |
q{this is another long string. I promise you at the end there will be a number, but I'm not going to let you have it right now. Ok, Now. 1}, | |
); | |
my %RE = ( | |
n => qr/[0-9]+/, | |
d => qr/\d+/, | |
); | |
cmpthese(500_000, { | |
'with o' => sub { /$RE{d}/o for @tests }, | |
'without o' => sub { /$RE{d}/ for @tests }, | |
}); |
Not quite accurate in the case of using predeclared regex.
Rate without o with o
without o 256410/s -- -41%
with o 434783/s 70% --
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
From: perldoc perlre
o - pretend to optimize your code, but actually introduce bugs