On 2016/01/30 21:17, mame / ruby-lang.org wrote:

> I have no strong objection, but still I have a question.  I guess that creating a large string by concatenating many short strings is natural use case in Ruby.  However, are there so much cases where we can predict the final length of a generated string before starting creating it?

Usually, when doing something like

result = ""
loop do
   result << some_string
end
result

this starts to get slow quite quickly. The standard advice (which I have 
myself used often) is to convert this to:

result = []
loop do
   result << some_string
end
result.join

This often speeds up things considerably.

I haven't completely analyzed the code for Array#join, but on first 
impression, it seems to do exactly what you suggest: calculate the 
overall length first, then do the actual copying.

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>