Tom Trebisky <tom / mmto.org> wrote:
[snip] 
> Then, I decided to use an array of lines rather than a really big string
>

server> ruby a.rb
  %   cumulative   self              self     total
 time   seconds   seconds    calls  ms/call  ms/call  name
 47.05     2.74      2.74        6   457.03   971.35  Integer#times
 32.17     4.62      1.88    10000     0.19     0.19  String#+
 10.19     5.21      0.59    10000     0.06     0.06  String#<<
  7.51     5.65      0.44     5000     0.09     0.09  Array#+
  3.08     5.83      0.18     5000     0.04     0.04  Array#<<
  0.13     5.84      0.01        1     7.81     7.81  Profiler__.start_profile
  0.00     5.84      0.00        1     0.00   679.69  Object#t_string_concat3
  0.00     5.84      0.00        1     0.00   835.94  Object#t_string_append3
  0.00     5.84      0.00        1     0.00   828.12  Object#t_array_append20
  0.00     5.84      0.00        1     0.00   664.06  Object#t_array_concat20
  0.00     5.84      0.00        6     0.00     0.00  Module#method_added
  0.00     5.84      0.00        1     0.00  2132.81  Object#t_string_append20
  0.00     5.84      0.00        1     0.00  5828.12  #toplevel
  0.00     5.84      0.00        1     0.00   687.50  Object#t_string_concat20
server> expand -t2 a.rb 
require 'profile'
def t_string_concat3
  str = ""
  5000.times{|n| str << "abc"}
end
def t_string_concat20
  str = ""
  5000.times{|n| str << "abcabcabcabcabcabcab"}
end
def t_string_append3
  str = ""
  5000.times{|n| str += "abc"}
end
def t_string_append20
  str = ""
  5000.times{|n| str += "abcabcabcabcabcabcab"}
end
def t_array_concat20
  ary = []
  5000.times{|n| ary << "abcabcabcabcabcabcab"}
end
def t_array_append20
  ary = []
  5000.times{|n| ary += ["abcabcabcabcabcabcab"] }
end

t_string_concat3
t_string_concat20
t_string_append3
t_string_append20
t_array_concat20
t_array_append20
server>


The fastest is Array.<<(element).

The slowest is String.+=(other_string). 

-- 
Simon Strandgaard