Skip to content

Instantly share code, notes, and snippets.

@reyjrar
Created May 28, 2015 11:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reyjrar/54c69ed1c7817368fa46 to your computer and use it in GitHub Desktop.
Save reyjrar/54c69ed1c7817368fa46 to your computer and use it in GitHub Desktop.
Regex Modifier: o
#!/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 },
});
@reyjrar
Copy link
Author

reyjrar commented May 28, 2015

From: perldoc perlre
o - pretend to optimize your code, but actually introduce bugs

@reyjrar
Copy link
Author

reyjrar commented May 28, 2015

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