From: Robert Klemme Date: 2004-12-31T04:26:43+09:00 Subject: Re: seeking hints for fast io "Eric Hodel" 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