Created
January 8, 2012 20:57
-
-
Save louismullie/1579661 to your computer and use it in GitHub Desktop.
Benchmark: Randomly selecting between N maximal values and returning key in Ruby hash.
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
Benchmark.bm do |x| | |
types = { | |
banana: 0.2, | |
lemon: 0.4, | |
orange: 0.4 | |
} | |
x.report("select") do | |
1.upto(10000) do | |
maxw = types.values.max | |
selection = types.select { |i,j| j == maxw }.keys.sample | |
end | |
end | |
x.report("group_by") do | |
1.upto(10000) do | |
selection = types.group_by {|k, v| v}.max.last.sample.first | |
end | |
end | |
end | |
# Results on Ruby 1.9.3 | |
# user system total real | |
# select 0.030000 0.000000 0.030000 ( 0.032520) | |
# group_by 0.070000 0.000000 0.070000 ( 0.072939) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment