Skip to content

Instantly share code, notes, and snippets.

@TonsOfFun
Created September 20, 2012 07:18
Show Gist options
  • Save TonsOfFun/3754387 to your computer and use it in GitHub Desktop.
Save TonsOfFun/3754387 to your computer and use it in GitHub Desktop.
String Vs Symbol Hashes
require 'benchmark'
n = 100_000
Benchmark.bm do |x|
x.report("string_hash:") do
n.times do
string_hash = {"test" => "some string"}
string_hash["test"]
end
end
x.report(:"symbol_hash:") do
n.times do
symbol_hash = {:test => :"some symbol"}
symbol_hash[:test]
end
end
x.report(:":mixed_hash:") do
n.times do
symbol_hash = {:test => "some symbol"}
symbol_hash[:test]
end
end
end
# Same string 3 times uses 3 objects
3.times { puts 'this is a string'.object_id}
# Same symbol 3 times uses 1 object
3.times { puts :'this is a string'.object_id}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment