Created
January 30, 2012 22:23
-
-
Save louismullie/1707180 to your computer and use it in GitHub Desktop.
Benchmark: Interning strings and string variables in Ruby
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
# :"test" is faster than "test".intern ... | |
Benchmark.bm do |x| | |
test = 'test' | |
x.report { 1000000.times { "test".intern } } | |
x.report { 1000000.times { :"test" } } | |
end | |
# user system total real | |
# 0.430000 0.000000 0.430000 ( 0.429735) | |
# 0.080000 0.000000 0.080000 ( 0.082169) | |
# But :"#{test}" is slower than test.intern. | |
Benchmark.bm do |x| | |
test = 'test' | |
x.report { 1000000.times { test.intern } } | |
x.report { 1000000.times { :"#{test}" } } | |
end | |
# user system total real | |
# 0.250000 0.000000 0.250000 ( 0.247637) | |
# 0.430000 0.000000 0.430000 ( 0.438851) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment