Skip to content

Instantly share code, notes, and snippets.

@ymhuang0808
Last active August 29, 2015 14:17
Show Gist options
  • Save ymhuang0808/641ce7a19b03cd62c8bd to your computer and use it in GitHub Desktop.
Save ymhuang0808/641ce7a19b03cd62c8bd to your computer and use it in GitHub Desktop.
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