[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Ruby "Speedup" hints?

Marc Heiler

3/17/2008 12:42:00 PM

Anyone of you has a few hints on how to speed up ruby code?
(Note - not writing it, but running it ;-) )

I only have a few hints, like 5... would love to extend it.

So without further ado:

- Using << instead of += for Strings as += creates a new object
whereas << will simply work on the current object.

- Use Inline C for critical methods (had to include that ;> )

- Reusing variable names might be better than using a lot of
different variables

- for is faster than .each on Arrays

- .last is faster than [0]

- .zero? is faster than == 0


If you know a few more hints, please add!
--
Posted via http://www.ruby-....

34 Answers

Robert Klemme

3/17/2008 1:02:00 PM

0

On 17.03.2008 13:42, Marc Heiler wrote:
> Anyone of you has a few hints on how to speed up ruby code?
> (Note - not writing it, but running it ;-) )
>
> I only have a few hints, like 5... would love to extend it.
>
> So without further ado:
>
> - Using << instead of += for Strings as += creates a new object
> whereas << will simply work on the current object.

I'd replace that with the general rule to avoid object creation because
that will cover more cases (Array#concat vs. Array#+, String#gsub! vs.
String#gsub etc.).

> - Use Inline C for critical methods (had to include that ;> )

You could argue that this does not speed up Ruby code but replaces it
with something else. So it's questionable whether this item should be
on the list.

> - Reusing variable names might be better than using a lot of
> different variables

Are you talking about a fact or a guess here? You write "might" - which
indicates to me that this is not a proven fact.

> - for is faster than .each on Arrays
>
> - .last is faster than [0]

I guess you meant Array#last is faster than Array#[-1] or Array#first is
faster than Array#[0].

> - .zero? is faster than == 0

Interesting, I did not know that. But the difference is really small:

robert@fussel ~
$ time ruby -e '1_000_000.times { 0.zero? }'

real 0m0.748s
user 0m0.468s
sys 0m0.108s

robert@fussel ~
$ time ruby -e '1_000_000.times { 1.zero? }'

real 0m0.748s
user 0m0.483s
sys 0m0.124s

robert@fussel ~
$ time ruby -e '1_000_000.times { 0 == 0 }'

real 0m0.869s
user 0m0.561s
sys 0m0.108s

robert@fussel ~
$ time ruby -e '1_000_000.times { 1 == 0 }'

real 0m0.857s
user 0m0.562s
sys 0m0.124s

robert@fussel ~
$

> If you know a few more hints, please add!

- freeze Strings that you are going to use as Hash keys.

Often bad design makes programs slow. While these are valid points
often the bigger effect can be achieved by proper designing an
application (i.e. use a Hash for frequent lookups instead of traversing
an Aarry).

Kind regards

robert

Ilan Berci

3/17/2008 1:23:00 PM

0

Marc Heiler wrote:
> Anyone of you has a few hints on how to speed up ruby code?
> (Note - not writing it, but running it ;-) )
>
> I only have a few hints, like 5... would love to extend it.
>

There is only 1 hint:

PROFILE IT!!!

everything else is BS..

hth

ilan

--
Posted via http://www.ruby-....

Sean T Allen

3/17/2008 1:58:00 PM

0


On Mar 17, 2008, at 9:04 AM, Robert Klemme wrote:

>> - Reusing variable names might be better than using a lot of
>> different variables
>
> Are you talking about a fact or a guess here? You write "might" -
> which indicates to me that this is not a proven fact.

and it certainly will eventually lead to programmer confusion esp when
dynamically scoped
blocks come into play.


M. Edward (Ed) Borasky

3/17/2008 2:01:00 PM

0

Marc Heiler wrote:
> Anyone of you has a few hints on how to speed up ruby code?
> (Note - not writing it, but running it ;-) )
>
> I only have a few hints, like 5... would love to extend it.
>
> So without further ado:
>
> - Using << instead of += for Strings as += creates a new object
> whereas << will simply work on the current object.
>
> - Use Inline C for critical methods (had to include that ;> )
>
> - Reusing variable names might be better than using a lot of
> different variables
>
> - for is faster than .each on Arrays
>
> - .last is faster than [0]
>
> - .zero? is faster than == 0
>
>
> If you know a few more hints, please add!

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

A whole book full of tips!


Avdi Grimm

3/17/2008 2:21:00 PM

0

On Mon, Mar 17, 2008 at 9:22 AM, Ilan Berci <coder68@yahoo.com> wrote:
> There is only 1 hint:
>
> PROFILE IT!!!

+1

All discussions of performance tuning are meaningless without profiling numbers.

--
Avdi

Paul Brannan

3/17/2008 3:07:00 PM

0

On Mon, Mar 17, 2008 at 11:20:59PM +0900, Avdi Grimm wrote:
> On Mon, Mar 17, 2008 at 9:22 AM, Ilan Berci <coder68@yahoo.com> wrote:
> > There is only 1 hint:
> >
> > PROFILE IT!!!
>
> +1
>
> All discussions of performance tuning are meaningless without
> profiling numbers.

Profiling only tells you where performance bottlenecks are. It does
nothing for letting you know how to fix those bottlenecks. That's where
benchmarks come into play.

Paul


Jano Svitok

3/17/2008 4:51:00 PM

0

On Mon, Mar 17, 2008 at 1:42 PM, Marc Heiler <shevegen@linuxmail.org> wrote:
> Anyone of you has a few hints on how to speed up ruby code?
> (Note - not writing it, but running it ;-) )
> If you know a few more hints, please add!

ParseDate#parsedate is expensive - it uses rationals, gcd and other
heavy stuff to convert from [D, M, Y, H, M, S] to timestamp.
Once we did a log merger and we ordered the entries by time. We saved
a lot of processing time by storing the timestamps in the logs along
with the formatted date.
(They were removed afterwards during formatting.)

ERB#new is expensive. Cache compiled templates if they are to be reused.

Use /o switch for Regexp literals that contain constant
#{substitutions} (i.e. that do not depend on function parameters)

Gerardo Santana Gómez Garrido

3/17/2008 5:17:00 PM

0

On Mon, Mar 17, 2008 at 12:42 PM, Marc Heiler <shevegen@linuxmail.org> wrote:
> Anyone of you has a few hints on how to speed up ruby code?
> (Note - not writing it, but running it ;-) )
>
> I only have a few hints, like 5... would love to extend it.

When generating text output, using StringIO is faster than using puts
--
Gerardo Santana

Ken Bloom

3/17/2008 5:38:00 PM

0

On Mon, 17 Mar 2008 14:01:51 +0100, Robert Klemme wrote:
> - freeze Strings that you are going to use as Hash keys.

Is that in any way a speedup hint? or is it just a safety hint? What
causes the speedup?

--Ken

--
Ken (Chanoch) Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology.
http://www.iit.edu...

Jano Svitok

3/17/2008 6:08:00 PM

0

On Mon, Mar 17, 2008 at 6:40 PM, Ken Bloom <kbloom@gmail.com> wrote:
> On Mon, 17 Mar 2008 14:01:51 +0100, Robert Klemme wrote:
> > - freeze Strings that you are going to use as Hash keys.
>
> Is that in any way a speedup hint? or is it just a safety hint? What
> causes the speedup?

Otherwise Hash will dup and freeze the String anyway.

http://ruby-doc.org/core/classes/Hash.ht... (What is not
mentioned there is that frozen strings will not be duplicated.)