Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Benchmark String and Hash in Ruby
require "benchmark"
precomputed_string = "Very long string value"*100
precomputed_symbol = precomputed_string.to_sym
MAP = {
"key1" => true,
:key2 => true,
precomputed_string => true,
precomputed_symbol => true
}
Benchmark.bm(20) do |x|
x.report("string") do
100000.times { MAP["key1"] }
end
x.report("symbol") do
100000.times { MAP[:key2] }
end
x.report("long string/100") do
100000.times { MAP[precomputed_string] }
end
x.report("long symbol") do
100000.times { MAP[precomputed_symbol] }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.