Created
December 12, 2013 22:06
-
-
Save rylwin/7936420 to your computer and use it in GitHub Desktop.
Compare '+' vs '\' for multiline string concatenation 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
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) |
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' | |
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