[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

seeking hints for fast io

jm

12/30/2004 11:04:00 AM

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
5 Answers

Eric Hodel

12/30/2004 6:22:00 PM

0

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://se...
FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04

Robert Klemme

12/30/2004 7:23:00 PM

0


"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

jm

12/30/2004 10:26:00 PM

0

I typed this from memory and forgot a line. sout is an array, but I'll
try what you've said and see if it helps any as there's no reason for
this to be an array instead of a string except for what I read
somewhere in the pickaxe book (p369, 2nd ed).

Jeff.

>> > def to_s

sout = Array.new()

>> > 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
>
>



jm

12/31/2004 1:15:00 AM

0

Did some simplistic benchmarking,
user cpu
original 8:49
array combining two lines 8:44
use string instead of array 8:46
change to string's % 8.49

not much variation. It's possible I'm looking in the wrong place. I'm
currently running the script with the profile module for a better idea
of where to look. Thanks to those who made suggestions.


Jeff.

On 31/12/2004, at 9:26 AM, jm wrote:

> I typed this from memory and forgot a line. sout is an array, but I'll
> try what you've said and see if it helps any as there's no reason for
> this to be an array instead of a string except for what I read
> somewhere in the pickaxe book (p369, 2nd ed).
>
> Jeff.



Robert Klemme

12/31/2004 12:40:00 PM

0


"jm" <jeffm@ghostgun.com> schrieb im Newsbeitrag
news:86269B85-5AC9-11D9-878B-000A95D0C980@ghostgun.com...
> Did some simplistic benchmarking,
> user cpu
> original 8:49
> array combining two lines 8:44
> use string instead of array 8:46
> change to string's % 8.49
>
> not much variation. It's possible I'm looking in the wrong place. I'm
> currently running the script with the profile module for a better idea of
> where to look. Thanks to those who made suggestions.

Just in case you didn't know it: there's also benchmark, which is less
intrusive than profile.

http://www.ruby-doc.org/stdlib/libdoc/benchmark/rdoc/...

Kind regards

robert