Skip to content

Instantly share code, notes, and snippets.

@reyjrar
Created December 8, 2013 12:20
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/7856658 to your computer and use it in GitHub Desktop.
Save reyjrar/7856658 to your computer and use it in GitHub Desktop.
Testing variations on matching digits with Perl Regex
#!/usr/bin/env perl
#
use strict;
use warnings;
use Benchmark qw(cmpthese);
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, {
'using \d' => sub { /$RE{d}/ for @tests },
'using \d with o' => sub { /$RE{d}/o for @tests },
'using \d with a' => sub { /$RE{d}/a for @tests },
'using [0-9]' => sub { /$RE{n}/ for @tests },
'using [0-9] with o' => sub { /$RE{n}/o for @tests },
});
@reyjrar
Copy link
Author

reyjrar commented Dec 8, 2013

$ perl regex.pl
                       Rate using \d using \d with a using [0-9] using \d with o using [0-9] with o
using \d           117925/s       --             -0%         -5%            -60%               -62%
using \d with a    118203/s       0%              --         -4%            -60%               -61%
using [0-9]        123762/s       5%              5%          --            -58%               -60%
using \d with o    297619/s     152%            152%        140%              --                -3%
using [0-9] with o 306748/s     160%            160%        148%              3%                 --

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment