Skip to content

Instantly share code, notes, and snippets.

@methodmissing
Created December 20, 2009 16:54
Show Gist options
  • Select an option

  • Save methodmissing/260555 to your computer and use it in GitHub Desktop.

Select an option

Save methodmissing/260555 to your computer and use it in GitHub Desktop.
require "benchmark"
$:.unshift "."
HASH = { :d => :d, 'e' => :e, 2 => 2 }
TESTS = 100_000
Benchmark.bmbm do |results|
results.report("Hash#[:sym]") { TESTS.times { HASH[:d] } }
results.report("Hash#[:str]") { TESTS.times { HASH['e'] } }
results.report("Hash#[:fix]") { TESTS.times { HASH[2] } }
end
methodmissing:ruby lourens$ ./ruby hash_bench.rb
Rehearsal -----------------------------------------------
Hash#[:sym] 0.020000 0.000000 0.020000 ( 0.014036)
Hash#[:str] 0.030000 0.000000 0.030000 ( 0.029462)
Hash#[:fix] 0.010000 0.000000 0.010000 ( 0.012764)
-------------------------------------- total: 0.060000sec
user system total real
Hash#[:sym] 0.010000 0.000000 0.010000 ( 0.013941)
Hash#[:str] 0.030000 0.000000 0.030000 ( 0.030123)
Hash#[:fix] 0.020000 0.000000 0.020000 ( 0.013026)
methodmissing:ruby lourens$ ruby hash_bench.rb
Rehearsal -----------------------------------------------
Hash#[:sym] 0.020000 0.000000 0.020000 ( 0.024361)
Hash#[:str] 0.040000 0.000000 0.040000 ( 0.034907)
Hash#[:fix] 0.020000 0.000000 0.020000 ( 0.025307)
-------------------------------------- total: 0.080000sec
user system total real
Hash#[:sym] 0.030000 0.000000 0.030000 ( 0.026289)
Hash#[:str] 0.040000 0.000000 0.040000 ( 0.035867)
Hash#[:fix] 0.020000 0.000000 0.020000 ( 0.025469)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment