-
-
Save hassox/3544925 to your computer and use it in GitHub Desktop.
Hash access speed with strings/vs symbols
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" | |
hash = {'key' => 1, :key => 2} | |
n = 5_000_000 | |
Benchmark.bm do |x| | |
x.report("strings") { n.times { hash['key'] } } | |
x.report("symbols") { n.times { hash[:key] } } | |
x.report("strings, set") { n.times { hash['key'] = 1 } } | |
x.report("symbols, set") { n.times { hash[:key] = 1 } } | |
end |
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
user system total real | |
strings 1.040000 0.010000 1.050000 ( 1.044701) | |
symbols 0.460000 0.000000 0.460000 ( 0.461030) | |
strings, set 1.250000 0.000000 1.250000 ( 1.253017) | |
symbols, set 0.690000 0.000000 0.690000 ( 0.689607) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I think removing the hash ops speaks for itself. It's all about the string allocation and symbol table searching.
user system total real
strings 1.170000 0.000000 1.170000 ( 1.172716)
symbols 0.310000 0.000000 0.310000 ( 0.316076)