in reply to Re: Behold! The power of recursion.
in thread Behold! The power of recursion.
It's further worth noting that DigitalKitty's algorithm uses only simple tail recursion, which can be easily compiled to be as fast or faster than an iterative solution.
Although I'm no expert, I believe the compiler would actually have to be smarter to optimize your iterative solution compared to the tail recursive one. Making the inference that $lower and $higher are candidates for register variables is slightly easier to make in the tail recursive version as there is a tendency to use registers for the subroutine calls variables for low-arity functions.
I could be wrong there, but a good compiler could do about as well with either, I would bet.
Now, implementing full tail call elimination is trickier, but recognizing tail recursion in this case and performing the optimization is a fairly trivial thing to do. Of course, we are talking about perl code here, and perl does not perform this optimization...
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^3: Behold! The power of recursion.
by tachyon (Chancellor) on Oct 18, 2004 at 04:10 UTC | |
by jordanh (Chaplain) on Oct 18, 2004 at 11:38 UTC | |
by tachyon (Chancellor) on Oct 18, 2004 at 22:19 UTC | |
by jordanh (Chaplain) on Oct 19, 2004 at 01:03 UTC | |
by tachyon (Chancellor) on Oct 19, 2004 at 04:02 UTC |