I am new to Ruby, and I have only known it through rails. I was
wondering if it's a good idea to use recursion sparingly in Ruby?
How efficiently is it implemented? Do recursive calls only store local
variables or the entire method?
Is there a maximum stack size/depth?
It's a good idea to recursion as much as you find convenient, and then test your code and see if it's too slow or not. If it's too slow, you have to figure out why, and you can put recursion on a long list of possible reasons.
If you have a specific algorithm you are considering doing recursively, you could easily test just that algorithm to see if it's fast enough.
-- Elliot Temple
···
On May 7, 2006, at 10:34 AM, Bob wrote:
Hi all,
I am new to Ruby, and I have only known it through rails. I was
wondering if it's a good idea to use recursion sparingly in Ruby?
How efficiently is it implemented?
Does anyone know if Ruby can optimize tail-recursions?
···
On 5/7/06, Bob <papersmith@gmail.com> wrote:
Hi all,
I am new to Ruby, and I have only known it through rails. I was
wondering if it's a good idea to use recursion sparingly in Ruby?
How efficiently is it implemented? Do recursive calls only store local
variables or the entire method?
Is there a maximum stack size/depth?
I'm more on James' side: since recursion doesn't really scale in Ruby
I try to avoid it where possible from the start.
13:16:04 [~]: ruby -e 'def r(i)puts i;r i+1 end; r 0'|tail
-e:1:in `r': stack level too deep (SystemStackError)
from -e:1:in `r'
from -e:1
12886
12887
12888
12889
12890
12891
12892
12893
12894
12895
13:16:06 [~]:
Kind regards
robert
···
2006/5/8, Elliot Temple <curi@curi.us>:
On May 7, 2006, at 10:34 AM, Bob wrote:
> Hi all,
>
> I am new to Ruby, and I have only known it through rails. I was
> wondering if it's a good idea to use recursion sparingly in Ruby?
> How efficiently is it implemented?
It's a good idea to recursion as much as you find convenient, and
then test your code and see if it's too slow or not. If it's too
slow, you have to figure out why, and you can put recursion on a long
list of possible reasons.
I like recursion, and it's very unfortunate that Ruby's recursion is pretty crappy. I say use it initially, then go to a loop-based implementation if you overflow the stack or find that it's too slow. I end up doing this about 10% of the time that I use recursion.