[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: seeking hints for fast io

e

12/31/2004 1:35:00 AM

> Lähettäjä: jm <jeffm@ghostgun.com>
> Päiväys: 2004/12/29 ke PM 05:04:06 GMT+02:00
> Vastaanottaja: ruby-talk@ruby-lang.org (ruby-talk ML)
> Aihe: seeking hints for fast io
>
> 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}"

So are you doing something like this now?

s << "a=" << @a << "\n" << "b=" << @b

You could try to just interpolate, which may or may not be faster.
There's no reason to have an explicit String variable, though, if
you're not storing it:

def to_s
"a=#{@a}\nb=#{@b}\n" # <- the 'empty line'
end

That's probably as fast as you're going to get. You say the data
is being manipulated so it's probably not possible for you to
prewrite/cache whatever @a and @b may be? The best thing you can
do is not write the data object by object like you're doing now,
but not knowing what you're *actually* doing we can't really offer
any concrete optimization advice.

> Jeff.

E