hi there-
so i started running into a problem using recursion with ruby- it seemed
a
little too soon for me to be getting a stack overflow (‘stack level too
deep
(SystemStackError)’), so i wrote up a quick simple script to do some
recursion:
#!/usr/local/bin/ruby
def print_depth num
puts num
print_depth(num+1)
end
print_depth 0
and sure enough, once im at a recursive depth of about 4,360, it errors
out.
i tried making num a global $num instead so it wasnt allocating memory
on
the stack each time for building a new ‘num’ object, but that only got
me to
about 4,500 until the overflow. to me, it seems a little low, especially
for
something this simple. my stack size is 10240. and, of course, when i up
it
to something like 20480, i get about double the recursive depth… but
when i
try the same thing in C:
#include <stdio.h>
void print_depth(int num);
int main() {
print_depth(0);
}
void print_depth(int num) {
printf("%d\n",num);
print_depth(++num);
}
i get a recursive depth of about 350,000 before it bombs out (which is
what
i’d expect more or less). i know C is super efficient and all, and ruby
is
truly object oriented, so each thing pushed on the stack is an object
with
its own set of methods, etc, so its by design going to take up more
space,
but i was wondering if there are any plans to do anything to help the
efficiency of this at all?
to be honest, i’ve only been using ruby for about a week now, and i
totally
love it. so, this e-mail really isn’t a complaint at all, just more of a
question. if there’s already an answer (which there probably is), feel
free
to shout it out- i love everything i’ve seen and done so far with the
language, so the more i know about it, the better! hope to hear from
someone
soon- thanks!
chet