Skip to content

Instantly share code, notes, and snippets.

@nihen
Created December 1, 2009 13:17
Show Gist options
  • Save nihen/246274 to your computer and use it in GitHub Desktop.
Save nihen/246274 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
use strict;
use warnings;
use Benchmark;
use Net::CIDR::Lite;
use Net::IP::Match::XS;
use Net::IP::Match::Bin;
use Net::IP::Match::Regexp;
use Net::Patricia;
use NetAddr::IP::Lite;
our @willcom_ip_public = (
"125.28.1.0/24",
"61.204.0.0/24",
"210.168.246.0/24",
"210.168.247.0/24",
"219.108.7.0/24",
"61.204.2.0/24",
"61.204.5.0/24",
"61.198.129.0/24",
"61.198.140.0/24",
"61.198.141.0/24",
"125.28.15.0/24",
"61.198.165.0/24",
"61.198.166.0/24",
"61.198.168.0/24",
"61.198.169.0/24",
"61.198.170.0/24",
"61.198.248.0/24",
"61.198.138.100/32",
"61.198.138.101/32",
"61.198.138.102/32",
"61.198.139.160/28",
"61.198.139.128/27",
"61.198.138.103/32",
"61.198.139.0/29",
"61.198.252.0/24",
"61.204.3.128/25",
"211.126.192.128/25",
);
my $count = 60000;
my $cidr = Net::CIDR::Lite->new;
$cidr->add_any($_) for @willcom_ip_public;
my $bin = Net::IP::Match::Bin->new();
$bin->add($_) for @willcom_ip_public;
my @cidr_list = $cidr->list;
my $re = Net::IP::Match::Regexp::create_iprange_regexp(@willcom_ip_public);
my $pat = Net::Patricia->new;
$pat->add_string($_) for @willcom_ip_public;
my @netaddr_ip_list = ();
push(@netaddr_ip_list, NetAddr::IP::Lite->new($_)) for @willcom_ip_public;
timethese( $count , {
'regexp' => sub {
Net::IP::Match::Regexp::match_ip('210.169.99.3',$re);
},
'cidr' => sub {
$cidr->find('210.169.99.3');
},
'xs' => sub {
Net::IP::Match::XS::match_ip('210.169.99.3',@willcom_ip_public);
},
'bin' => sub {
$bin->match_ip('210.169.99.3');
},
'patricia' => sub {
$pat->match_string('210.169.99.3');
},
'netaddr_ip_lite' => sub {
my $request_ip = NetAddr::IP::Lite->new('210.169.99.3');
for ( @netaddr_ip_list ) {
return if $request_ip->within($_);
}
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment