Skip to content

Instantly share code, notes, and snippets.

@NullVoxPopuli
Last active August 11, 2017 12:31
Show Gist options
  • Save NullVoxPopuli/2cfbc5c28e2170530b25f279b7e8375d to your computer and use it in GitHub Desktop.
Save NullVoxPopuli/2cfbc5c28e2170530b25f279b7e8375d to your computer and use it in GitHub Desktop.
Ruby Hash Access Comparisons
require 'benchmark/ips'
require "active_support/core_ext/hash/indifferent_access"
symbols = {
a: 1
}
strings = {
'a'.freeze => 1
}
un_frozen_strings = {
"a" => 1
}
indifferent = {
a: 1,
'b'.freeze => 2,
"c" => 3
}.with_indifferent_access
Benchmark.ips do |x|
x.config(time: 5, warmup: 2, iterations: 1)
x.report('symbols') { symbols[:a] }
x.report('string') { strings['a'] }
x.report('unfrozen strings') { strings["a"] }
x.report('indifferent - string -> symbol') { indifferent['a'] }
x.report('indifferent - symbol -> string') { indifferent[:b] }
x.report('indifferent - symbol -> unfrozen') { indifferent[:c] }
x.report('indifferent - symbol -> symbol') { indifferent[:a] }
x.report('indifferent - string -> string') { indifferent['b'] }
x.compare!
end
@NullVoxPopuli
Copy link
Author

NullVoxPopuli commented Aug 11, 2017

Warming up --------------------------------------
             symbols   405.379k i/100ms
              string   375.505k i/100ms
    unfrozen strings   376.377k i/100ms
indifferent - string -> symbol
                       236.913k i/100ms
indifferent - symbol -> string
                       217.321k i/100ms
indifferent - symbol -> unfrozen
                       218.162k i/100ms
indifferent - symbol -> symbol
                       220.186k i/100ms
indifferent - string -> string
                       234.896k i/100ms
Calculating -------------------------------------
             symbols     11.637M (± 4.2%) i/s -     58.375M in   5.026784s
              string      9.389M (± 4.0%) i/s -     46.938M in   5.008397s
    unfrozen strings      9.544M (± 6.8%) i/s -     47.424M in   5.000332s
indifferent - string -> symbol
                          3.799M (± 3.7%) i/s -     19.190M in   5.059159s
indifferent - symbol -> string
                          3.332M (± 8.2%) i/s -     16.734M in   5.066330s
indifferent - symbol -> unfrozen
                          3.068M (±10.7%) i/s -     15.271M in   5.044249s
indifferent - symbol -> symbol
                          3.140M (± 8.7%) i/s -     15.633M in   5.022583s
indifferent - string -> string
                          3.683M (± 9.7%) i/s -     18.322M in   5.033870s

Comparison:
             symbols: 11636774.0 i/s
    unfrozen strings:  9543525.5 i/s - 1.22x  slower
              string:  9388657.8 i/s - 1.24x  slower
indifferent - string -> symbol:  3798541.1 i/s - 3.06x  slower
indifferent - string -> string:  3682729.3 i/s - 3.16x  slower
indifferent - symbol -> string:  3332172.0 i/s - 3.49x  slower
indifferent - symbol -> symbol:  3139989.6 i/s - 3.71x  slower
indifferent - symbol -> unfrozen:  3068479.7 i/s - 3.79x  slower

System:
MacBookPro11,5
16GiB System Memory
Intel(R) Core(TM) i7-4870HQ CPU @ 2.50GHz

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