$ cat 791149.pl use strict; use warnings; use Net::IP::Match::Regexp qw( create_iprange_regexp match_ip ); use Benchmark qw / cmpthese /; my $regexp_init_once = create_iprange_regexp( qw( 10.0.0.0/8 172.16.0.0/12 192.168.0.0/16 ) ); sub netip { my $regexp = create_iprange_regexp( qw( 10.0.0.0/8 172.16.0.0/12 192.168.0.0/16 ) ); return ( match_ip( $_[0], $regexp ) ); } sub netip_init_once { return match_ip( $_[0], $regexp_init_once ); } sub regexp { return $_[0] =~ /^(10\.\d+|172\.(1[6-9]|2\d|3[0-1])|192\.168)(\.\d+){2}$/; } cmpthese( -1, { 'netip' => sub { netip('1.2.3.4') }, 'netip_init_once' => sub { netip_init_once('1.2.3.4') }, 'regexp' => sub { regexp('1.2.3.4') }, } ); __END__ $ perl 791149.pl Rate netip netip_init_once regexp netip 2438/s -- -96% -100% netip_init_once 67622/s 2674% -- -95% regexp 1390157/s 56918% 1956% --