Skip to content

Instantly share code, notes, and snippets.

@dLobatog
Created April 20, 2012 10:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dLobatog/2427600 to your computer and use it in GitHub Desktop.
Save dLobatog/2427600 to your computer and use it in GitHub Desktop.
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