Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Benchmark
require "benchmark"
foo = "foo"
bar = "bar"
array = [foo, bar]
hash = {:a => foo, :b => bar}
nested_hash = Hash.new { |h,k| h[k] = {} }
nested_hash[foo][bar] = true
array_hash = { array => true }
hash_hash = { hash => true }
Benchmark.realtime { 1000.times { nested_hash[foo][bar] } } # => 0.000474
Benchmark.realtime { 1000.times { array_hash[array] } } # => 0.000900
Benchmark.realtime { 1000.times { hash_hash[hash] } } # => 0.001364
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment