Instantly share code, notes, and snippets.

Embed
What would you like to do?
require 'benchmark/ips'
str = { "foo" => 1 }
sym = { foo: 1 }
Benchmark.ips do |x|
x.report("string") { str["foo"] }
x.report("symbol") { sym[:foo] }
x.compare!
end
#### Results (Ruby 2.4) ####
# Warming up --------------------------------------
# string 153.784k i/100ms
# symbol 180.471k i/100ms
# Calculating -------------------------------------
# string 5.493M (± 3.2%) i/s - 27.527M in 5.016529s
# symbol 9.323M (± 3.8%) i/s - 46.562M in 5.001791s
#
# Comparison:
# symbol: 9323414.3 i/s
# string: 5493159.4 i/s - 1.70x slower
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment