Created
September 20, 2012 07:18
-
-
Save TonsOfFun/3754387 to your computer and use it in GitHub Desktop.
String Vs Symbol Hashes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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