Created
January 12, 2010 23:48
-
-
Save weppos/275768 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'ipaddr' | |
require 'benchmark' | |
class IPAddr | |
def self.valid_ipv4?(addr) | |
if /\A(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\Z/ =~ addr | |
return $~.captures.all? {|i| i.to_i < 256} | |
end | |
false | |
end | |
def self.valid_ipv6?(addr) | |
# IPv6 (normal) | |
return true if /\A[\dA-Fa-f]{1,4}(:[\dA-Fa-f]{1,4})*\Z/ =~ addr | |
return true if /\A[\dA-Fa-f]{1,4}(:[\dA-Fa-f]{1,4})*::([\dA-Fa-f]{1,4}(:[\dA-Fa-f]{1,4})*)?\Z/ =~ addr | |
return true if /\A::([\dA-Fa-f]{1,4}(:[\dA-Fa-f]{1,4})*)?\Z/ =~ addr | |
# IPv6 (IPv4 compat) | |
return true if /\A[\dA-Fa-f]{1,4}(:[\dA-Fa-f]{1,4})*:/ =~ addr && valid_ipv4?($') | |
return true if /\A[\dA-Fa-f]{1,4}(:[\dA-Fa-f]{1,4})*::([\dA-Fa-f]{1,4}(:[\dA-Fa-f]{1,4})*:)?/ =~ addr && valid_ipv4?($') | |
return true if /\A::([\dA-Fa-f]{1,4}(:[\dA-Fa-f]{1,4})*:)?/ =~ addr && valid_ipv4?($') | |
false | |
end | |
end | |
class Bim | |
attr_accessor :cls, :data | |
def initialize(cls, data) | |
self.cls = cls | |
self.data = data | |
end | |
def value | |
IPAddr.new(data).to_i | |
rescue ArgumentError | |
data | |
end | |
end | |
class Bum | |
attr_accessor :cls, :data | |
def initialize(cls, data) | |
self.cls = cls | |
self.data = data | |
end | |
def value | |
if cls == "A" | |
IPAddr.new(data).to_i | |
else | |
data | |
end | |
end | |
end | |
class Bam | |
attr_accessor :cls, :data | |
def initialize(cls, data) | |
self.cls = cls | |
self.data = data | |
end | |
def value | |
if IPAddr.valid_ipv4?(data) || IPAddr.valid_ipv6?(data) | |
IPAddr.new(data).to_i | |
else | |
data | |
end | |
end | |
end | |
TIMES = 50_000 | |
Benchmark.bmbm do |x| | |
x.report("NS exception") do | |
TIMES.times { b = Bim.new("NS", "ns2.foo.com"); b.value } | |
end | |
x.report("NS if") do | |
TIMES.times { b = Bum.new("NS", "ns2.foo.com"); b.value } | |
end | |
x.report("NS regexp") do | |
TIMES.times { b = Bam.new("NS", "ns2.foo.com"); b.value } | |
end | |
x.report("A exception") do | |
TIMES.times { b = Bim.new("A", "192.168.1.1"); b.value } | |
end | |
x.report("A if") do | |
TIMES.times { b = Bum.new("A", "192.168.1.1"); b.value } | |
end | |
x.report("A regepx") do | |
TIMES.times { b = Bum.new("A", "192.168.1.1"); b.value } | |
end | |
end | |
__END__ | |
# ruby 1.8.7 (2009-12-24 patchlevel 248) [i686-darwin10.2.0] | |
$ ruby if_vs_exception.rb | |
Rehearsal ------------------------------------------------ | |
NS exception 4.330000 5.750000 10.080000 ( 37.599575) | |
NS if 0.140000 0.010000 0.150000 ( 0.136280) | |
NS regexp 0.450000 0.000000 0.450000 ( 0.454040) | |
A exception 2.010000 0.020000 2.030000 ( 2.047711) | |
A if 2.060000 0.020000 2.080000 ( 2.074642) | |
A regepx 2.030000 0.020000 2.050000 ( 2.054930) | |
-------------------------------------- total: 16.840000sec | |
user system total real | |
NS exception 4.420000 5.810000 10.230000 ( 38.350608) | |
NS if 0.130000 0.000000 0.130000 ( 0.130863) | |
NS regexp 0.450000 0.000000 0.450000 ( 0.447975) | |
A exception 2.000000 0.020000 2.020000 ( 2.016231) | |
A if 2.020000 0.020000 2.040000 ( 2.043085) | |
A regepx 2.030000 0.020000 2.050000 ( 2.048402) | |
# ruby 1.9.1p376 (2009-12-07 revision 26041) [i386-darwin10.2.0] | |
Rehearsal ------------------------------------------------ | |
NS exception 4.650000 5.800000 10.450000 ( 39.134858) | |
NS if 0.080000 0.000000 0.080000 ( 0.079427) | |
NS regexp 0.320000 0.000000 0.320000 ( 0.320389) | |
A exception 1.180000 0.010000 1.190000 ( 1.182892) | |
A if 1.200000 0.000000 1.200000 ( 1.209433) | |
A regepx 1.220000 0.000000 1.220000 ( 1.219345) | |
-------------------------------------- total: 14.460000sec | |
user system total real | |
NS exception 4.520000 5.720000 10.240000 ( 37.931455) | |
NS if 0.080000 0.000000 0.080000 ( 0.078286) | |
NS regexp 0.320000 0.000000 0.320000 ( 0.320988) | |
A exception 1.190000 0.000000 1.190000 ( 1.186198) | |
A if 1.210000 0.010000 1.220000 ( 1.220254) | |
A regepx 1.220000 0.000000 1.220000 ( 1.215634) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'ipaddr' | |
require 'benchmark' | |
class IPAddr | |
def self.valid_ipv4?(addr) | |
if /\A(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\Z/ =~ addr | |
return $~.captures.all? {|i| i.to_i < 256} | |
end | |
false | |
end | |
def self.valid_ipv6?(addr) | |
# IPv6 (normal) | |
return true if /\A[\dA-Fa-f]{1,4}(:[\dA-Fa-f]{1,4})*\Z/ =~ addr | |
return true if /\A[\dA-Fa-f]{1,4}(:[\dA-Fa-f]{1,4})*::([\dA-Fa-f]{1,4}(:[\dA-Fa-f]{1,4})*)?\Z/ =~ addr | |
return true if /\A::([\dA-Fa-f]{1,4}(:[\dA-Fa-f]{1,4})*)?\Z/ =~ addr | |
# IPv6 (IPv4 compat) | |
return true if /\A[\dA-Fa-f]{1,4}(:[\dA-Fa-f]{1,4})*:/ =~ addr && valid_ipv4?($') | |
return true if /\A[\dA-Fa-f]{1,4}(:[\dA-Fa-f]{1,4})*::([\dA-Fa-f]{1,4}(:[\dA-Fa-f]{1,4})*:)?/ =~ addr && valid_ipv4?($') | |
return true if /\A::([\dA-Fa-f]{1,4}(:[\dA-Fa-f]{1,4})*:)?/ =~ addr && valid_ipv4?($') | |
false | |
end | |
end | |
class Bim | |
attr_accessor :cls, :data | |
def initialize(cls, data) | |
self.cls = cls | |
self.data = data | |
end | |
def value | |
1 / 0 | |
rescue ZeroDivisionError | |
1 | |
end | |
end | |
class Bum | |
attr_accessor :cls, :data | |
def initialize(cls, data) | |
self.cls = cls | |
self.data = data | |
end | |
def value | |
if cls == "A" | |
0 | |
else | |
1 | |
end | |
end | |
end | |
class Bam | |
attr_accessor :cls, :data | |
def initialize(cls, data) | |
self.cls = cls | |
self.data = data | |
end | |
def value | |
if IPAddr.valid_ipv4?(data) || IPAddr.valid_ipv6?(data) | |
0 | |
else | |
1 | |
end | |
end | |
end | |
TIMES = 50_000 | |
Benchmark.bmbm do |x| | |
x.report("NS exception") do | |
TIMES.times { b = Bim.new("NS", "ns2.foo.com"); b.value } | |
end | |
x.report("NS if") do | |
TIMES.times { b = Bum.new("NS", "ns2.foo.com"); b.value } | |
end | |
x.report("NS regexp") do | |
TIMES.times { b = Bam.new("NS", "ns2.foo.com"); b.value } | |
end | |
x.report("A exception") do | |
TIMES.times { b = Bim.new("A", "192.168.1.1"); b.value } | |
end | |
x.report("A if") do | |
TIMES.times { b = Bum.new("A", "192.168.1.1"); b.value } | |
end | |
x.report("A regepx") do | |
TIMES.times { b = Bum.new("A", "192.168.1.1"); b.value } | |
end | |
end | |
__END__ | |
$ ruby /Users/carletti/Desktop/bench.rb | |
Rehearsal ------------------------------------------------ | |
NS exception 0.690000 0.050000 0.740000 ( 0.740209) | |
NS if 0.100000 0.000000 0.100000 ( 0.100608) | |
NS regexp 0.380000 0.000000 0.380000 ( 0.379736) | |
A exception 0.690000 0.040000 0.730000 ( 0.735069) | |
A if 0.100000 0.000000 0.100000 ( 0.099597) | |
A regepx 0.100000 0.000000 0.100000 ( 0.104152) | |
--------------------------------------- total: 2.150000sec | |
user system total real | |
NS exception 0.690000 0.050000 0.740000 ( 0.746834) | |
NS if 0.100000 0.000000 0.100000 ( 0.099066) | |
NS regexp 0.370000 0.000000 0.370000 ( 0.380592) | |
A exception 0.690000 0.050000 0.740000 ( 0.741232) | |
A if 0.100000 0.000000 0.100000 ( 0.100776) | |
A regepx 0.100000 0.000000 0.100000 ( 0.101338) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment