performance tips

V

Vince Forgetta

[Note: parts of this message were removed to make it a legal post.]

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
 
J

Joel VanderWerf

Vince said:
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.
 
M

M. Edward (Ed) Borasky

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

Well, there's actually a book on the subject ...

http://www.informit.com/store/product.aspx?isbn=0321540034

I recommend it highly!
 
V

Vince Forgetta

[Note: parts of this message were removed to make it a legal post.]

perfect! Thanks.

Vince
 
G

Gennady Bystritsky

[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 Forgetta [[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 suc=
h
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
 
P

Phillip Gawlowski

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

Gennady Bystritsky 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 Programming Language by O'Reilly. And technically, Matz is the
co-author, not the sole author of the book.

See also: http://www.oreilly.com/catalog/9780596516178/


- --
Phillip Gawlowski
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-----
 
G

Gennady Bystritsky

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

Gennady said:
[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 Programming Language by O'Reilly. And technically, Matz is
the co-author, not the sole author of the book.

See also: http://www.oreilly.com/catalog/9780596516178/

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

Gennady.
 
M

Maran Chandrasekar

Joel said:
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?
 
R

Robert Klemme

2008/4/10 Joel VanderWerf said:
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.

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

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
 
J

Joel VanderWerf

Robert said:
??? 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 Link for pointing that out as well in
an email.
 
S

Siep Korteling

Vince said:
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
 
J

jonty

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

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

jonty

jonty said:
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!
 

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
473,772
Messages
2,569,593
Members
45,112
Latest member
VinayKumar Nevatia
Top