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

Jeff.

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

PGP.sig (186 Bytes)

···

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

--
Eric Hodel - drbrain@segment7.net - http://segment7.net
FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04

"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

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

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.

"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/index.html

Kind regards

    robert