Skip to content

Instantly share code, notes, and snippets.

@gmanley
Last active July 11, 2017 18:34
Show Gist options
  • Save gmanley/6626758 to your computer and use it in GitHub Desktop.
Save gmanley/6626758 to your computer and use it in GitHub Desktop.
require 'benchmark'
begin
require 'active_support/values/time_zone'
rescue LoadError
puts 'Please do `gem install active_support`'
exit(1)
end
def benchmark_hash(hash, key, inverted_hash)
Benchmark.bmbm do |x|
x.report('finding in hash by value using select') do
hash.select { |k, v| v == key }.keys.first
end
x.report('finding in hash by value using find') do
hash.find { |k, v| v == key }.first
end
x.report('transposing & finding in hash by key') do
hash.invert[key]
end
x.report('finding in hash by key using pre-inverted hash') do
inverted_hash[key]
end
end
end
nums = 1..1000000
hash = nums.inject({}) do |memo, n|
memo["key_#{n}"] = "value_#{n}"
memo
end
printf "\e[1;31m Unrealistic dataset:\e[0m\n\n"
benchmark_hash(hash, "value_#{rand(nums.max)}", hash.invert)
printf "\n\e[1;31m Realistic dataset:\e[0m\n\n"
benchmark_hash(ActiveSupport::TimeZone::MAPPING, 'America/Chicago', hash.invert)
# ruby -e "$(curl -Ls http://git.io/8eHTIA)"
Unrealistic dataset:
Rehearsal ----------------------------------------------------------------------------------
finding in hash by value using select 0.360000 0.000000 0.360000 ( 0.359564)
finding in hash by value using find 0.170000 0.010000 0.180000 ( 0.178563)
transposing & finding in hash by key 1.900000 0.040000 1.940000 ( 1.937345)
finding in hash by key using pre-inverted hash 0.000000 0.000000 0.000000 ( 0.000004)
------------------------------------------------------------------------- total: 2.480000sec
user system total real
finding in hash by value using select 0.350000 0.000000 0.350000 ( 0.356475)
finding in hash by value using find 0.170000 0.000000 0.170000 ( 0.163680)
transposing & finding in hash by key 1.730000 0.010000 1.740000 ( 1.745643)
finding in hash by key using pre-inverted hash 0.000000 0.000000 0.000000 ( 0.000007)
Realistic dataset:
Rehearsal ----------------------------------------------------------------------------------
finding in hash by value using select 0.000000 0.000000 0.000000 ( 0.000029)
finding in hash by value using find 0.000000 0.000000 0.000000 ( 0.000007)
transposing & finding in hash by key 0.000000 0.000000 0.000000 ( 0.000048)
finding in hash by key using pre-inverted hash 0.000000 0.000000 0.000000 ( 0.000003)
------------------------------------------------------------------------- total: 0.000000sec
user system total real
finding in hash by value using select 0.000000 0.000000 0.000000 ( 0.000034)
finding in hash by value using find 0.000000 0.000000 0.000000 ( 0.000014)
transposing & finding in hash by key 0.000000 0.000000 0.000000 ( 0.000062)
finding in hash by key using pre-inverted hash 0.000000 0.000000 0.000000 ( 0.000006)
@seattle-jim
Copy link

Cool way to find the truth! Maybe you can give a talk on those cool ruby features some day!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment