Skip to content

Instantly share code, notes, and snippets.

@louismullie
Created January 30, 2012 22:23
Show Gist options
  • Save louismullie/1707180 to your computer and use it in GitHub Desktop.
Save louismullie/1707180 to your computer and use it in GitHub Desktop.
Benchmark: Interning strings and string variables in Ruby
# :"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