Skip to content

Instantly share code, notes, and snippets.

@rylwin
Created December 12, 2013 22:06
Show Gist options
  • Save rylwin/7936420 to your computer and use it in GitHub Desktop.
Save rylwin/7936420 to your computer and use it in GitHub Desktop.
Compare '+' vs '\' for multiline string concatenation in Ruby
Running with n of 1000000
user system total real
slash: 0.170000 0.010000 0.180000 ( 0.164066)
plus: 0.430000 0.000000 0.430000 ( 0.439272)
interpolate: 0.170000 0.000000 0.170000 ( 0.161643)
require 'benchmark'
def multiline_with_slash
'hello world'\
' how are you today?'
end
def multiline_with_plus
'hello world' +
' how are you today?'
end
def multiline_with_interpolate
"#{'hello world'} how are you today?"
end
n = 1000000
puts "Running with n of #{n}"
Benchmark.bm(12) do |x|
x.report("slash:") { n.times { multiline_with_slash } }
x.report("plus:") { n.times { multiline_with_plus } }
x.report("interpolate:") { n.times { multiline_with_interpolate } }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment