Skip to content

Instantly share code, notes, and snippets.

@cowens
Last active August 29, 2015 14:13
Show Gist options
  • Save cowens/5314bda172a58fa1e230 to your computer and use it in GitHub Desktop.
Save cowens/5314bda172a58fa1e230 to your computer and use it in GitHub Desktop.
use strict;
use warnings;
use Benchmark;
my $s = "aaaa 192.168.0.1 sdf";
my %subs = (
norm => sub {
return scalar $s =~ /(a)(a)(a)(a)/;
},
nocap => sub {
return scalar $s =~ /(a)(a)(a)(a)/n;
},
manual => sub {
return scalar $s =~ /(?:a)(?:a)(?:a)(?:a)/;
}
);
print "simple\n";
Benchmark::cmpthese(-1, \%subs);
%subs = (
norm => sub {
return scalar $s =~ /
\b
(
[0-9] |
[1-9][0-9] |
1[0-9][0-9] |
2 ( [0-4][0-9] | 5[0-5] )
[.]
){3}
(
[0-9] |
[1-9][0-9] |
1[0-9][0-9] |
2 ( [0-4][0-9] | 5[0-5] )
)
\b
/x;
},
nocap => sub {
return scalar $s =~ /
\b
(
[0-9] |
[1-9][0-9] |
1[0-9][0-9] |
2 ( [0-4][0-9] | 5[0-5] )
[.]
){3}
(
[0-9] |
[1-9][0-9] |
1[0-9][0-9] |
2 ( [0-4][0-9] | 5[0-5] )
)
\b
/xn;
},
manual => sub {
return scalar $s =~ /
\b
(?:
[0-9] |
[1-9][0-9] |
1[0-9][0-9] |
2 (?: [0-4][0-9] | 5[0-5] )
[.]
){3}
(?:
[0-9] |
[1-9][0-9] |
1[0-9][0-9] |
2 (?: [0-4][0-9] | 5[0-5] )
)
\b
/x;
},
);
print "IP addresses\n";
Benchmark::cmpthese(-1, \%subs);
__DATA__
simple
Rate norm nocap manual
norm 3855059/s -- -68% -68%
nocap 11993516/s 211% -- -0%
manual 11993516/s 211% 0% --
IP addresses
Rate norm nocap manual
norm 267962/s -- -11% -13%
nocap 300755/s 12% -- -3%
manual 309688/s 16% 3% --
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment