Robert Klemme
12/30/2004 7:23:00 PM
"Eric Hodel" <drbrain@segment7.net> schrieb im Newsbeitrag
news:A589DDC3-5A8F-11D9-8CD7-000D93436DA0@segment7.net...
> On 30 Dec 2004, at 03:04, jm wrote:
>
> > I have a class which holds data while it's being manipulated. The
> > trouble is that I call to_s() quite a few times printing the resulting
> > data to a file. This increases the run time of the script a surprising
> > amount raising the runtime from about 6 minutes to about 8 and a half.
> > The code looks roughly likely the following,
> >
> > class Example
> > .
> > .
> > def to_s
> > sout << "a=#{@a}"
> > sout << "b=#{@b}"
> > sout.join("\n")
> > end
> > end
> >
> > # in main
> > File.open(outfile,File::CREAT|File::RDWR|File::APPEND) do |fp|
> > the_data.each do |d|
> > fp.puts d
> > fp.puts # add blank line
> > end
> > end
> >
> > Anyone care to suggest a way to speed this up? Is it faster to
> >
> > sout << "a=" + @a
> >
> > instead of
> >
> > sout << "a=#{@a}"
>
> Probably not, String#+ creates new strings, String#<< appends to an
> existing string
This is quite simple and avoids the unnecessary string interpolation. I
guess it'll be faster:
sout << "a=" << @a
Kind regards
robert