Created
July 10, 2018 14:51
-
-
Save smileart/2744ac4990fa7f60f7453bb9508ce9c4 to your computer and use it in GitHub Desktop.
Benchmarking different RangedHash implementations
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 'benchmark/ips' | |
class RangedHashSelect | |
def initialize(hash) | |
@data = Hash[ | |
hash.keys.map do |el| | |
el.respond_to?(:member?) ? el : (el..el) | |
end.zip(hash.values) | |
] | |
end | |
def [](index) | |
@data.select {|i| i === index }.values.first | |
end | |
end | |
class RangedHashEach | |
def initialize(hash) | |
@data = Hash[ | |
hash.keys.map do |el| | |
el.respond_to?(:member?) ? el : (el..el) | |
end.zip(hash.values) | |
] | |
end | |
def [](index) | |
@data.each do |range, value| | |
return value if range.include?(index) | |
end | |
end | |
end | |
class RangedHashEachThrequeal | |
def initialize(hash) | |
@data = Hash[ | |
hash.keys.map do |el| | |
el.respond_to?(:member?) ? el : (el..el) | |
end.zip(hash.values) | |
] | |
end | |
def [](index) | |
@data.each do |range, value| | |
return value if range === index | |
end | |
end | |
end | |
class RangedHashEachBetween | |
def initialize(hash) | |
@data = Hash[ | |
hash.keys.map do |el| | |
el.respond_to?(:include?) ? el : [el, el] | |
end.zip(hash.values) | |
] | |
end | |
def [](index) | |
@data.each do |r, value| | |
return value if index.between?(r[0], r[1]) | |
end | |
end | |
end | |
h = { | |
0 => 'a', | |
1 => 'b', | |
(2..9) => 'c', | |
10 => 'd' | |
} | |
ah = { | |
0 => 'a', | |
1 => 'b', | |
[2, 9] => 'c', | |
10 => 'd' | |
} | |
p RangedHashSelect.new(h)[3] | |
p RangedHashEach.new(h)[3] | |
p RangedHashEachThrequeal.new(h)[3] | |
p RangedHashEachBetween.new(ah)[3] | |
# exit | |
p "="*50 | |
Benchmark.ips do |x| | |
x.config(:time => 10, :warmup => 5) | |
x.report("select") { | |
RangedHashSelect.new(h) | |
} | |
x.report("each/===") { | |
RangedHashEachThrequeal.new(h) | |
} | |
x.report("between?") { | |
RangedHashEachBetween.new(ah) | |
} | |
x.report("each/include?") { | |
RangedHashEach.new(h) | |
} | |
end | |
3.times { puts } | |
Benchmark.ips do |x| | |
x.config(:time => 10, :warmup => 5) | |
rhs = RangedHashSelect.new(h) | |
x.report("select") { | |
rhs[rand(2..9)] | |
} | |
rhet = RangedHashEachThrequeal.new(h) | |
x.report("each/===") { | |
rhet[rand(2..9)] | |
} | |
rheb = RangedHashEachBetween.new(ah) | |
x.report("between?") { | |
rheb[rand(2..9)] | |
} | |
rhe = RangedHashEach.new(h) | |
x.report("each/include?") { | |
rhe[rand(2..9)] | |
} | |
end | |
3.times { puts } | |
Benchmark.ips do |x| | |
x.config(:time => 10, :warmup => 5) | |
rhs = RangedHashSelect.new(h) | |
x.report("select") { | |
rhs[3] | |
} | |
rhet = RangedHashEachThrequeal.new(h) | |
x.report("each/===") { | |
rhet[3] | |
} | |
rheb = RangedHashEachBetween.new(ah) | |
x.report("between?") { | |
rheb[3] | |
} | |
rhe = RangedHashEach.new(h) | |
x.report("each/include?") { | |
rhe[3] | |
} | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment