On 7/26/07, Kaldrenon <kaldrenon / gmail.com> wrote:
> On Jul 26, 3:32 am, "Jano Svitok" <jan.svi... / gmail.com> wrote:
> > For this I meant n.to_f/d that was there three times. I'm not sure if
> > ruby reuses the value or computes it three times.
>
> In other words, if the exact same calculation is needed in more than
> one place, store it instead of recalculating? Gotta remember that
> one...

Right, but it depends on calculation's complexity. sometimes creating
a variable for it could be longer that the calculation itself. For
best results, always use profile and Benchmark class.

For me profiler says, most of the time is spent in gcd calculation.
So, further optimizations should go into either avoiding gcd as much
as possible, speeding it up and/or caching its results somehow
(sometimes you can trade memory complexity for space). I haven't found
any such optimizations.

Final note: this was an exercise in optimizing as much as possible. In
normal code one does not do such things, as this would complicate the
maintainability of the code. It's important to weight speed vs.
complexity of the code...

J.