Não é mais possível fazer postagens ou usar assinaturas novas da Usenet nos Grupos do Google. O conteúdo histórico continua disponível.
Dismiss

Small optimization tips

14 visualizações
Pular para a primeira mensagem não lida

Vincent Foley

não lida,
14 de mar. de 2006, 13:53:0914/03/2006
para
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-forum.com/.


Charlie Bowman

não lida,
14 de mar. de 2006, 13:59:5714/03/2006
para
I made a script run 800% faster by using the Time class instead of the
DateTime class. You can see the scripts and more details at
http://www.recentrambles.com/pragmatic/view/33 Also, using a "memoize"
function whenever possible can greatly increase performance.


I'm still new to ruby, but what does this mean?

On Wed, 2006-03-15 at 03:53 +0900, Vincent Foley wrote:

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

Charlie Bowman
http://www.recentrambles.com

James Edward Gray II

não lida,
14 de mar. de 2006, 14:04:1414/03/2006
para
On Mar 14, 2006, at 12:59 PM, Charlie Bowman wrote:

> I'm still new to ruby, but what does this mean?

The Set library is a standard library that comes with Ruby. I
believe it uses rbtree if it can be loaded or a plain Hash otherwise,
but it supports the expected set operations and keeps the contents
unique.

James Edward Gray II


Charlie Bowman

não lida,
14 de mar. de 2006, 14:17:2814/03/2006
para
Thanks, I'll look that up in the pickaxe book when I get home.

Charlie Bowman
Programmer
Castle Branch Inc.

Vincent Foley

não lida,
14 de mar. de 2006, 14:22:3114/03/2006
para
Ah thanks about the Time class. The script I wrote deals with dates,
and I was using Date.strptime. Switching to Time.gm increased the
speed of the script by 4x.

ara.t....@noaa.gov

não lida,
14 de mar. de 2006, 14:57:0114/03/2006
para

when, oh when, will rbtree be in the core... sigh.

-a
--
share your knowledge. it's a way to achieve immortality.
- h.h. the 14th dali lama


Joel VanderWerf

não lida,
14 de mar. de 2006, 15:14:1114/03/2006
para
ara.t....@noaa.gov wrote:
> On Wed, 15 Mar 2006, James Edward Gray II wrote:
>
>> On Mar 14, 2006, at 12:59 PM, Charlie Bowman wrote:
>>
>>> I'm still new to ruby, but what does this mean?
>>
>> The Set library is a standard library that comes with Ruby. I believe
>> it uses rbtree if it can be loaded or a plain Hash otherwise, but it
>> supports the expected set operations and keeps the contents unique.
>
> when, oh when, will rbtree be in the core... sigh.
>
> -a

and it would be nice for it to have some more methods, like #slice! (a
destructive version of #bound).

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407


Robert Klemme

não lida,
14 de mar. de 2006, 15:20:5414/03/2006
para
2006/3/14, Vincent Foley <vfoley...@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/fussel-foto/


James Edward Gray II

não lida,
14 de mar. de 2006, 15:30:2914/03/2006
para
On Mar 14, 2006, at 1:57 PM, ara.t....@noaa.gov wrote:

> On Wed, 15 Mar 2006, James Edward Gray II wrote:
>
>> On Mar 14, 2006, at 12:59 PM, Charlie Bowman wrote:
>>
>>> I'm still new to ruby, but what does this mean?
>>
>> The Set library is a standard library that comes with Ruby. I
>> believe it uses rbtree if it can be loaded or a plain Hash
>> otherwise, but it supports the expected set operations and keeps
>> the contents unique.
>
> when, oh when, will rbtree be in the core... sigh.

Isn't it the library someone was complaining is broken in Ruby
1.8.4? We might need to fix it first. <laughs>

James Edward Gray II


Shea Martin

não lida,
14 de mar. de 2006, 15:34:5214/03/2006
para

> 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

Joel VanderWerf

não lida,
14 de mar. de 2006, 15:39:3414/03/2006
para
James Edward Gray II wrote:
> Isn't it the library someone was complaining is broken in Ruby 1.8.4?
> We might need to fix it first. <laughs>

Did you mean ruby-talk:183166 ? I've been using rbtree happily on 1.8.4.

James Edward Gray II

não lida,
14 de mar. de 2006, 15:47:0814/03/2006
para
On Mar 14, 2006, at 2:39 PM, Joel VanderWerf wrote:

> James Edward Gray II wrote:
>> Isn't it the library someone was complaining is broken in Ruby 1.8.4?
>> We might need to fix it first. <laughs>
>
> Did you mean ruby-talk:183166 ? I've been using rbtree happily on
> 1.8.4.

Oops, yeah, my memory was poor on that one. It looks like it might
be broken on 1.8.2, not 1.8.4. Sorry. My bad.

James Edward Gray II


John Carter

não lida,
14 de mar. de 2006, 17:32:1914/03/2006
para
On Wed, 15 Mar 2006, Vincent Foley wrote:

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

instead of this...

open( file_name) do |inf|
inf.each_line do |line|
line.chomp! # Optimization Tip, use chomp! instead of
# line = line.chomp
next unless line =~ %r{Looks right}x
#do stuff
end
end

try...

File.read( file_name).scan(%r{ what I'm actually looking for}mx) do |m|
# Do _stuff
end

Much much faster!

Always remember change of algorithm can get you 2 orders of magnitude
speed, optimization tweaks usually around factors 1.5

No code is faster than No Code.

Native code is very much faster than Ruby.

eg. I wrote a ring buffer class in Ruby but found unless it grew to about
2000 elements using Array.shift and unshift was faster.

Benchmark class is the the optimizers friend.

John Carter Phone : (64)(3) 358 6639
Tait Electronics Fax : (64)(3) 359 4632
PO Box 1645 Christchurch Email : john....@tait.co.nz
New Zealand

Carter's Clarification of Murphy's Law.

"Things only ever go right so that they may go more spectacularly wrong later."

From this principle, all of life and physics may be deduced.


Vincent Foley

não lida,
15 de mar. de 2006, 07:28:5515/03/2006
para
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 Edward Gray II

não lida,
15 de mar. de 2006, 08:54:3015/03/2006
para

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

não lida,
15 de mar. de 2006, 09:13:1215/03/2006
para

"Shea Martin" <null@void.0> wrote in message
news:MNFRf.43456$VV4.6...@ursa-nb00s0.nbnet.nb.ca...

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

não lida,
16 de mar. de 2006, 23:01:4716/03/2006
para
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.

ps. Anyone worked out how to use gcc profile feedback to build a faster
ruby yet?

Eric Hodel

não lida,
4 de abr. de 2006, 22:05:2004/04/2006
para
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 - drb...@segment7.net - http://blog.segment7.net
This implementation is HODEL-HASH-9600 compliant

http://trackmap.robotcoop.com


0 nova mensagem