I recall getting a scolding for concatenating Strings in Python once upon a time. I was told that it is more efficient to create an List of Strings in Python and join them later. I carried this practice over into JavaScript and Ruby although I am unsure if this has the same benefit in latter.
Can anyone tell me if it is more efficient (resource and execution-wise) to join a Array of Strings and call :join on them or to concatenate a string as needed in the Ruby programming language?
Thanks.
Doing N concatenations requires creating N new strings in the process. join() , on the other hand, only has to create a single string (the final result) and thus works much faster.
Concatenation adds two strings and join separates the strings from Array.
String join is significantly faster then concatenation.
You can think of string concatenation as gluing strings together. And, you can think of string interpolation without strings as injecting strings inside of other strings.
Try it yourself with the Benchmark class.
require "benchmark" n = 1000000 Benchmark.bmbm do |x| x.report("concatenation") do foo = "" n.times do foo << "foobar" end end x.report("using lists") do foo = [] n.times do foo << "foobar" end string = foo.join end end
This produces the following output:
Rehearsal ------------------------------------------------- concatenation 0.300000 0.010000 0.310000 ( 0.317457) using lists 0.380000 0.050000 0.430000 ( 0.442691) ---------------------------------------- total: 0.740000sec user system total real concatenation 0.260000 0.010000 0.270000 ( 0.309520) using lists 0.310000 0.020000 0.330000 ( 0.363102)
So it looks like concatenation is a little faster in this case. Benchmark on your system for your use-case.
Funny, benchmarking gives surprising results (unless I'm doing something wrong):
require 'benchmark' N = 1_000_000 Benchmark.bm(20) do |rep| rep.report('+') do N.times do res = 'foo' + 'bar' + 'baz' end end rep.report('join') do N.times do res = ['foo', 'bar', 'baz'].join end end rep.report('<<') do N.times do res = 'foo' << 'bar' << 'baz' end end end
gives
jablan@poneti:~/dev/rb$ ruby concat.rb user system total real + 1.760000 0.000000 1.760000 ( 1.791334) join 2.410000 0.000000 2.410000 ( 2.412974) << 1.380000 0.000000 1.380000 ( 1.376663)
join
turns out to be the slowest. It might have to do with creating the array, but that's what you would have to do anyway.
Oh BTW,
jablan@poneti:~/dev/rb$ ruby -v ruby 1.9.1p378 (2010-01-10 revision 26273) [i486-linux]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With