Skip to content

Instantly share code, notes, and snippets.

@mmozuras
Created November 7, 2014 21:40
Show Gist options
  • Save mmozuras/62c87312eda6201e2ba0 to your computer and use it in GitHub Desktop.
Save mmozuras/62c87312eda6201e2ba0 to your computer and use it in GitHub Desktop.
Benchmark hash access in Ruby: symbol versus string
require 'benchmark/ips'
string_hash = { 'aaa' => 1, 'bbb' => 2, 'ccc' => 3 }
symbol_hash = { aaa: 1, bbb: 2, ccc: 3 }
Benchmark.ips do |x|
x.report('string') { string_hash['bbb'] }
x.report('symbol') { symbol_hash[:bbb] }
x.compare!
end
@mmozuras
Copy link
Author

mmozuras commented Nov 7, 2014


          symbol    101287 i/100ms
          string     80688 i/100ms

          symbol  6180027.1 (±6.9%) i/s -   30791248 in   5.013438s
          string  3341525.2 (±5.6%) i/s -   16702416 in   5.018036s

          symbol:  6180027.1 i/s
          string:  3341525.2 i/s - 1.85x slower

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment