Skip to content

Instantly share code, notes, and snippets.

@maasha
Created September 12, 2014 12:28
Show Gist options
  • Save maasha/1c108f9d45c1de917b1c to your computer and use it in GitHub Desktop.
Save maasha/1c108f9d45c1de917b1c to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'inline'
require 'benchmark'
hash = {foo: "bar"}
n = 10_000_000
Benchmark.bm() do |x|
x.report("hash[]") { n.times { hash[:foo] } }
x.report("hash.has_key?") { n.times { hash.has_key? :foo } }
end
maasha@mel:~$ ./benchmark.rb
user system total real
hash[] 1.100000 0.000000 1.100000 ( 1.111618)
hash.has_key? 1.360000 0.000000 1.360000 ( 1.364842)
maasha@mel:~$ rbx benchmark.rb
user system total real
hash[] 2.341919 0.019072 2.360991 ( 2.272809)
hash.has_key? 1.915534 0.002146 1.917680 ( 1.860527)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment