Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

commented May 28, 2015

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

@reyjrar

This comment has been minimized.

Copy link
Owner Author

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
You can’t perform that action at this time.