Performance tips

Hi all,

I’ve been using ruby for about 2 years, but only recently got around
trying
and learning it well.

Is there some central repository I can get performance tips/discussions
such
as the one posted here:

http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/158212

These types of tips are saving my bacon!

I’m reading through Matz’s new book as well … what a difference this
book
has made!

Thanks.

Vince

Vince F. wrote:

Hi all,

I’ve been using ruby for about 2 years, but only recently got around trying
and learning it well.

Is there some central repository I can get performance tips/discussions such
as the one posted here:

http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/158212

Instead of:

lines = File.readlines(‘sometextfile’).each {|l| l.chomp!}

do this:

lines = File.open “test” do |f|
f.map {|line| line.chomp}
end

That saves you from having the large string returned by #readlines.

Sorry, no idea if these tips are collected somewhere. The rubygarden
wiki used to have them, IIRC, but it got killed by spam.

Vince F. wrote:

These types of tips are saving my bacon!

I’m reading through Matz’s new book as well … what a difference this book
has made!

Thanks.

Vince

Well, there’s actually a book on the subject …

I recommend it highly!

perfect! Thanks.

Vince

On Thu, Apr 10, 2008 at 10:05 PM, M. Edward (Ed) Borasky
[email protected]

Vince F. wrote:

perfect! Thanks.

Vince

On Thu, Apr 10, 2008 at 10:05 PM, M. Edward (Ed) Borasky
[email protected]

up

visit a rubyonrails website :http://www.rorchina.net
wolf union program club :http://wolf.rorchina.net
China Rubyonrails club: http://bbs.rorchina.net

[Sorry for the top-posting, freaking web-mail interface does not do
quoting properly]

Can you elaborate on what Matz’s new book you referring to in your post?
I could not find anything by Matz in English other than “Ruby in a
Nutshell”.

Gennady.


From: Vince F. [[email protected]]
Sent: Thursday, April 10, 2008 1:30 PM
To: ruby-talk ML
Subject: performance tips

Hi all,

I’ve been using ruby for about 2 years, but only recently got around
trying
and learning it well.

Is there some central repository I can get performance tips/discussions
such
as the one posted here:

http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/158212

These types of tips are saving my bacon!

I’m reading through Matz’s new book as well … what a difference this
book
has made!

Thanks.

Vince

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Gennady B. wrote:
| [Sorry for the top-posting, freaking web-mail interface does not do
quoting properly]
|
| Can you elaborate on what Matz’s new book you referring to in your
post? I could not find anything by Matz in English other than “Ruby in a
Nutshell”.

The Ruby P.ming Language by O’Reilly. And technically, Matz is the
co-author, not the sole author of the book.

See also: The Ruby Programming Language [Book]


Phillip G.
Twitter: twitter.com/cynicalryan

~ There’s never enough time to do all the nothing you want.
– Calvin
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.8 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAkf+47wACgkQbtAgaoJTgL9LmwCfShQaZKNzXljKKRXj2toDfBN0
axAAoKjcWX/07xNntg4jh8jIAeo7kX8t
=QTYm
-----END PGP SIGNATURE-----

Phillip G. wrote:

The Ruby P.ming Language by O’Reilly. And technically, Matz is
the co-author, not the sole author of the book.

See also: The Ruby Programming Language [Book]

Thanks a lot, Phillip!!! I will check it out today.

Gennady.

Joel VanderWerf wrote:

Vince F. wrote:

Hi all,

I’ve been using ruby for about 2 years, but only recently got around trying
and learning it well.

Is there some central repository I can get performance tips/discussions such
as the one posted here:

http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/158212

Instead of:

lines = File.readlines(‘sometextfile’).each {|l| l.chomp!}

require ‘benchmark’
include Benchmark

bm(7){|test|
test.report('report : '){
lines = File.readlines(‘LOG/development.log’).each {|l|
l.chomp!}
}
}

Output:
user system total real
report : 0.090000 0.030000 0.120000 ( 0.117250)

Actually ‘development.log’ file 6MB size

do this:

lines = File.open “test” do |f|
f.map {|line| line.chomp}
end

require ‘benchmark’
include Benchmark
bm(7){|test|
test.report('report : '){
lines = File.open “LOG/development.log” do |f|
f.map {|line| line.chomp}
end
}
}
Output :
user system total real
report : 0.240000 0.040000 0.280000 ( 0.272991)

which is better?

That saves you from having the large string returned by #readlines.

Sorry, no idea if these tips are collected somewhere. The rubygarden
wiki used to have them, IIRC, but it got killed by spam.

Robert K. wrote:

as the one posted here:
end

That saves you from having the large string returned by #readlines.

??? I could have understood if you argued that your solution avoids
traversing lines twice but this statement is plain wrong:
File.readlines never returned a single String.

Oops, I think possibly I meant File.read and map:

lines = File.read(‘sometextfile’).map {|l| l.chomp!}

Thanks to you, Robert, and to Tom L. for pointing that out as well in
an email.

2008/4/10 Joel VanderWerf [email protected]:

as the one posted here:
f.map {|line| line.chomp}
end

That saves you from having the large string returned by #readlines.

??? I could have understood if you argued that your solution avoids
traversing lines twice but this statement is plain wrong:
File.readlines never returned a single String.

http://www.ruby-doc.org/core/classes/IO.html#M002269

Sorry, no idea if these tips are collected somewhere. The rubygarden wiki
used to have them, IIRC, but it got killed by spam.

Many performance considerations can be found here. Searching for
“Benchmark” should get you pretty far. :slight_smile:

Apart from that IMHO there are two major rules of performance which
can help eliminate many performance issues:

  1. Do not do unnecessary work.

  2. Carefully choose object life cycles.

Kind regards

robert

Vince F. wrote:

Hi all,

Is there some central repository I can get performance tips/discussions
(…)
Thanks.

Vince

Don’t use Date objects if you can get by with Time. (As a bonus, if you
start using Time today, you might catch Time.now.to_i == 1234567890)

regards,

Siep

jonty wrote:

tips/discussions
regards,

Siep

Thanks!!!

I’ve set up scite with puts Time.now.to_i and am happily pressing
f5 every so often watching it creep up to the magic number!

I did it! 1234567890! Am I sad or what!

Siep K. wrote:

Thanks.

Thanks!!!

I’ve set up scite with puts Time.now.to_i and am happily pressing f5
every so often watching it creep up to the magic number!