Skip to content

Instantly share code, notes, and snippets.

@codahale
Created March 19, 2010 03:02
Show Gist options
  • Save codahale/337186 to your computer and use it in GitHub Desktop.
Save codahale/337186 to your computer and use it in GitHub Desktop.
# ruby 1.8.7 (2009-06-12 patchlevel 174) [i686-darwin10.2.0], MBARI 0x6770, Ruby Enterprise Edition 2009.10
# user system total real
# hash lookup: 21.470000 0.120000 21.590000 ( 23.683924)
# method dispatch: 26.980000 0.150000 27.130000 ( 29.014672)
# jruby 1.4.0 (ruby 1.8.7 patchlevel 174) (2010-01-12 6586) (Java HotSpot(TM) 64-Bit Server VM 1.6.0_17) [x86_64-java]
# user system total real
# hash lookup: 6.548000 0.000000 6.548000 ( 6.548000)
# method dispatch: 10.841000 0.000000 10.841000 ( 10.841000)
require "benchmark"
S = "yay"
class One
def yay
S
end
end
one = One.new
X = {
:yay => S
}
TESTS = 100_000_000
Benchmark.bmbm do |results|
results.report("hash lookup:") { TESTS.times { X[:yay] } }
results.report("method dispatch:") { TESTS.times { one.yay } }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment