Skip to content

Instantly share code, notes, and snippets.

@bayashi
Created March 1, 2015 00:28
Show Gist options
  • Save bayashi/ffc7edd26f609af54c46 to your computer and use it in GitHub Desktop.
Save bayashi/ffc7edd26f609af54c46 to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
use strict;
use warnings;
use utf8;
use Benchmark qw(cmpthese);
my $h = '25';
my $pattern = '((?:(?:'.$h.'[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))';
my $regexp = qr/$pattern/;
my $regexp_o = qr/$pattern/o;
my $ipv4 = '192.168.1.1';
cmpthese(0, +{
'precompile' => sub {
$ipv4 =~ $regexp;
},
'precompile_o' => sub {
$ipv4 =~ $regexp_o;
},
'runtime' => sub {
$ipv4 =~ /$pattern/;
},
'runtime_optimized' => sub {
$ipv4 =~ /$pattern/o;
},
'runtime_static' => sub {
$ipv4 =~ /((?:(?:$h[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))/;
},
'runtime_static_o' => sub {
$ipv4 =~ /((?:(?:$h[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))/o;
},
});
@i110
Copy link

i110 commented Mar 1, 2015

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