Skip to content

Instantly share code, notes, and snippets.

@tenderlove
Created February 18, 2016 02:09
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 tenderlove/49649d4a26425d351cfd to your computer and use it in GitHub Desktop.
Save tenderlove/49649d4a26425d351cfd to your computer and use it in GitHub Desktop.
require 'benchmark/ips'
class Foo
alias :object_hash :hash
attr_reader :hash
def initialize
@hash = object_hash
end
end
class Bar
end
hash = {}
foo = Foo.new
bar = Bar.new
Benchmark.ips do |x|
x.report("foo") { hash[foo] }
x.report("bar") { hash[bar] }
x.report("foo.hash") { foo.hash }
x.report("bar.hash") { bar.hash }
end
__END__
[aaron@TC ruby (trunk)]$ ruby test.rb
Warming up --------------------------------------
foo 118.361k i/100ms
bar 118.637k i/100ms
Calculating -------------------------------------
foo 7.944M (± 3.1%) i/s - 39.769M
bar 7.931M (± 3.4%) i/s - 39.625M
[aaron@TC ruby (trunk)]$ ruby test.rb
Warming up --------------------------------------
foo 122.180k i/100ms
bar 120.492k i/100ms
foo.hash 123.397k i/100ms
bar.hash 119.312k i/100ms
Calculating -------------------------------------
foo 8.002M (± 4.2%) i/s - 39.953M
bar 8.037M (± 4.5%) i/s - 40.124M
foo.hash 8.819M (± 3.9%) i/s - 44.053M
bar.hash 7.856M (± 4.1%) i/s - 39.254M
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment