Paul Butcher wrote:

> That's a very surprising result! I, too, would be interested in any 
> light that anyone can cast on it.
> 

Replacing

 def cost
   who.collect(&:time).max
 end

 with

 who.inject(0) { |current_max, toy| toy.time > current_max ? toy.time : 
current_max }

makes paul's solution run 5.5 times faster on my machine (in around 29 
seconds). Still slower than Brian's (which takes 24 seconds), but a 
difference of 20% or so is less surprising that multiples.

Cost is obviously called a lot, the intial version iterates over who 
once, creates a new array then iterates over it again to get the max. 
I'm guessing the creation of lots of garbage & the second iteration are 
the big factors.

Fred
-- 
Posted via http://www.ruby-forum.com/.