String concatenation in ruby

Hi,

This seems to be a basic question but i would like to know why.

String concatenation in rails can be done in the following three ways:

  1. a+b+c
  2. a<<b<<c
  3. “#{a}#{b}#{c}”

where a b c are three variables

Which is the good way to go about concatenation and why?

a+b+c is not good because it creates temp string objects i guess

But i would really like to know of the three which is better and why. Is
there any performance difference in using them

Thanks in advance

On 08/05/2010 06:47 AM, Prachi T. wrote:

where a b c are three variables

Which is the good way to go about concatenation and why?

a+b+c is not good because it creates temp string objects i guess

But i would really like to know of the three which is better and why. Is
there any performance difference in using them

Thanks in advance

As you mention, using + will create strings which then need to be
garbage collected. Using << will modify the left-most variable, which
you may not always desire. The other downside to using + or << is that
they will raise an error if any of your variables are not strings. If
you use interpolation, to_s will be called on the variables which may
save your program from crashing.

Interpolation also appears to be the fastest method:
http://rubybenchmark.com/reports/6

-Justin

On Thu, Aug 5, 2010 at 2:47 PM, Prachi T.
[email protected]wrote:

there any performance difference in using them
Thanks in advance

You can experiment using benchmark (see below) or hitimes (which I
haven’t
tried yet), and there may well be others. I think the results should be
taken more as indications rather than definitive, but you can see from
the
following that whether one way is faster than another can depend on the
data. (I hope that the formatting of the times is ok - it looks
diabolical
in this draft.)

require “benchmark”
r1 = r2 = r3 = nil

a = nil
av = “Karel Capek”.freeze
b = " wrote"
c = " War with the Newts"
kt = 100_000
Benchmark.bmbm do |r|
r.report(“a+b+c” ) {kt.times { a = av.dup; r1 = a + b + c } }
r.report(“a<<b<<c” ) {kt.times { a = av.dup; r2 = a << b << c } }
r.report(’#{a}#{b}#{c}’) {kt.times { a = av.dup; r3 = “#{a}#{b}#{c}” } }
end
p r1, r2, r3

av = (“kc " * 100).freeze
b = " wr” * 50
c = " WN" * 50
kt = 100_000
Benchmark.bmbm do |r|
r.report(“a+b+c” ) {kt.times { a = av.dup; r1 = a + b + c } }
r.report(“a<<b<<c” ) {kt.times { a = av.dup; r2 = a << b << c } }
r.report(’#{a}#{b}#{c}’) {kt.times { a = av.dup; r3 = “#{a}#{b}#{c}” } }
end

##########=>
run: “C:\ruby19\bin\ruby.exe” -v “aaa-bm-str.rb”
ruby 1.9.1p243 (2009-07-16 revision 24175) [i386-mingw32]

Rehearsal ------------------------------------------------
a+b+c 0.405000 0.000000 0.405000 ( 0.470000)
a<<b<<c 0.406000 0.000000 0.406000 ( 0.410000)
#{a}#{b}#{c} 0.468000 0.000000 0.468000 ( 0.515000)
--------------------------------------- total: 1.279000sec
user system total real
a+b+c 0.452000 0.000000 0.452000 ( 0.465000)
a<<b<<c 0.390000 0.000000 0.390000 ( 0.395000)
#{a}#{b}#{c} 0.499000 0.000000 0.499000 ( 0.520000)
“Karel Capek wrote War with the Newts”
“Karel Capek wrote War with the Newts”
“Karel Capek wrote War with the Newts”

Rehearsal ------------------------------------------------
a+b+c 0.624000 0.016000 0.640000 ( 0.690000)
a<<b<<c 0.499000 0.000000 0.499000 ( 0.515000)
#{a}#{b}#{c} 0.671000 0.000000 0.671000 ( 0.650000)
--------------------------------------- total: 1.810000sec
user system total real
a+b+c 0.639000 0.000000 0.639000 ( 0.645000)
a<<b<<c 0.515000 0.031000 0.546000 ( 0.540000)
#{a}#{b}#{c} 0.624000 0.000000 0.624000 ( 0.680000)

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs