On Fri, 13 Jun 2003, Dave Thomas wrote:

> I don't believe there's a single fastest way.
>
> Often '<<' is fast, but not always.
>
> In one application, where I was generating a CVS file from a largish SQL
> result set, I made the program over 100 times faster by changing
>
>     result = ""
>     for r in results
>        csv_string = to_csv(r)
>        result << csv_string
>     end
>
> to
>
>     result = []
>     for r in results
>        csv_string = to_csv(r)
>        result << csv_string
>     end
>     result = result.join
>
For "Ruby Developer's Guide" I did some measurements on this and join was
often significantly faster when there are many strings to
concat. Also "<<" was somewhat but not much faster than "+=" (for many
strings, for fewer string "+=" was sometimes faster). Performance
measurement is hairy business though and its hard to generalise... ;)

Regards,

Robert Feldt