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.carter / 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.