seeking hints for fast io

J

jm

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
 
E

Eric Hodel

--Apple-Mail-27--577836336
Content-Transfer-Encoding: 7bit
Content-Type: text/plain; charset=US-ASCII; format=flowed

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 - (e-mail address removed) - 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--
 
R

Robert Klemme

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
 
J

jm

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.

sout = Array.new()
 
J

jm

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

Robert Klemme

jm said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,164
Messages
2,570,898
Members
47,439
Latest member
shasuze

Latest Threads

Top