Last active
July 11, 2017 18:34
-
-
Save gmanley/6626758 to your computer and use it in GitHub Desktop.
This file contains hidden or 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' | |
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)" |
This file contains hidden or 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
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Cool way to find the truth! Maybe you can give a talk on those cool ruby features some day!