[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Small optimization tips

Vincent Foley

3/14/2006 6:53:00 PM

Hello all,

I was working on a little script earlier, which I thought was too slow
for my taste. It's now about 5-6x faster than it originally was.
Normally, this would've been enough, but being very curious about Ruby,
I tried to make it go even faster by using micro optimizations. Here
are some things I found out:

* Running your script with the -rprofile option is the first thing you
should do
* Using a Set instead of an Array when you only want to store unique
values can help make your code go faster
* Disabling the GC can slightly increase the speed of your script.
However, I don't think it's a good idea to use in larger applications

That's a few things I found out today. Does anyone else have quick
optimization tips?

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


8 Answers

Robert Klemme

3/14/2006 8:21:00 PM

0

2006/3/14, Vincent Foley <vfoleybourgon@yahoo.ca>:
> Hello all,
>
> I was working on a little script earlier, which I thought was too slow
> for my taste. It's now about 5-6x faster than it originally was.
> Normally, this would've been enough, but being very curious about Ruby,
> I tried to make it go even faster by using micro optimizations. Here
> are some things I found out:
>
> * Running your script with the -rprofile option is the first thing you
> should do

... if it's too slow.

> * Using a Set instead of an Array when you only want to store unique
> values can help make your code go faster

IMHO this is not a micro optimization, this is a changed design. A set
represents something entirely different from an array (although they
share some properties).

> * Disabling the GC can slightly increase the speed of your script.
> However, I don't think it's a good idea to use in larger applications

I don't think either.

> That's a few things I found out today. Does anyone else have quick
> optimization tips?

In no particular order: Create as few objects as possible. Watch out
where you can in place modifying methods of string vs. non bang
methods. Choose the appropriate abstractions. Don't prematurely
optimize.

Kind regards

robert


--
Have a look: http://www.flickr.com/photos/fu...


Shea Martin

3/14/2006 8:35:00 PM

0


> In no particular order: Create as few objects as possible. Watch out
> where you can in place modifying methods of string vs. non bang
> methods. Choose the appropriate abstractions. Don't prematurely
> optimize.

Just clarification: you are saying in-place is faster right?
~S

John Carter

3/14/2006 10:32:00 PM

0

Vincent Foley

3/15/2006 12:29:00 PM

0

I have something else that is probably common knowledge: if you can
avoid using regular expressions, do it. By replacing the regex matches
in my script with string operations, I increased the runtime speed by a
factor of ~4.

James Gray

3/15/2006 1:55:00 PM

0

On Mar 15, 2006, at 6:33 AM, Vincent Foley wrote:

> I have something else that is probably common knowledge: if you can
> avoid using regular expressions, do it. By replacing the regex
> matches
> in my script with string operations, I increased the runtime speed
> by a
> factor of ~4.

However, if you can replace a few Ruby operations with one
complicated search and replace, sometimes it works the other way. It
depends on the problem.

James Edward Gray II



Robert Klemme

3/15/2006 2:13:00 PM

0


"Shea Martin" <null@void.0> wrote in message
news:MNFRf.43456$VV4.659574@ursa-nb00s0.nbnet.nb.ca...
>
>> In no particular order: Create as few objects as possible. Watch out
>> where you can in place modifying methods of string vs. non bang
>> methods. Choose the appropriate abstractions. Don't prematurely
>> optimize.
>
> Just clarification: you are saying in-place is faster right?

Right. But not applicable everywhere (i.e. if you're not allowed to modify
something or do not know whether something is frozen...).

Kind regards

robert

John Carter

3/17/2006 4:02:00 AM

0

Eric Hodel

4/5/2006 2:05:00 AM

0

On Mar 16, 2006, at 8:01 PM, John Carter wrote:

> On Wed, 15 Mar 2006, Vincent Foley wrote:
>
>> That's a few things I found out today. Does anyone else have quick
>> optimization tips?
>
> Use ruby-1.9 it is faster.
> Compile with gcc-4.1
> Optimize
>
> CC=gcc-4.1 CFLAGS='-march=pentium4 -O3 -fomit-frame-pointer'
> CPP=cpp-4.1 > CXXFLAGS='-march=pentium4 -O3 -fomit-frame-pointer' 'CXX=g++-4.1' > ./configure
> make
>
>
> Warning, this may be hazardous for your health, but it works for me.

You don't want to compile with -fomit-frame-pointer. See [ruby-core:
7660]

--
Eric Hodel - drbrain@segment7.net - http://blog.se...
This implementation is HODEL-HASH-9600 compliant

http://trackmap.rob...