From: Eric Hodel Date: 2004-12-31T03:22:22+09:00 Subject: Re: seeking hints for fast io --Apple-Mail-27--577836336 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=US-ASCII; format=flowed 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 You can eliminate most of the method calls there though: def to_s "a=#{@a}\nb=#{@b}" end (I realize this is probably a small example of what really happens.) -- Eric Hodel - drbrain@segment7.net - http://segment7.net FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04 --Apple-Mail-27--577836336 content-type: application/pgp-signature; x-mac-type=70674453; name=PGP.sig content-description: This is a digitally signed message part content-disposition: inline; filename=PGP.sig content-transfer-encoding: 7bit -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.4 (Darwin) iD8DBQFB1EczMypVHHlsnwQRApHRAKDf210aOCnnnpAwYNaCkPMLoNYIWgCeJ7dZ dBUxvsyHmelU+jfCY81kVx0= =Sq49 -----END PGP SIGNATURE----- --Apple-Mail-27--577836336--