maximum recursion depth

Fernando Perez fperez528 at yahoo.com
Sun Jul 21 20:07:09 EDT 2002


> I think I will rewrite my code using loops. This will be possible, I just
> have to put my braincells back in active mode :-)
> I found that a too deep recursion will slow down executing to much.
> For now my program works with the recursion limit set at 3000.
> However I can think of situations where this will not be enough.

Recursion is expensive because function calls are expensive (in general, 
but especially so in python). Unwinding a stack 3000 layers deep is just 
  asking for trouble. In most cases recursive algorithms are a good 
choice when you know that you won't go too deep, and when the actual 
work done at each call is substantial enough that the overhead of 
calling a function can be ignored. If either of those conditions isn't 
met, it's probably time to rethink the problem.

You may want to look at generators. Since they don't have to build/uwind 
the stack, they may fit nicely for certain classes of problems. There's 
a very nice article on generators at IBM Developer Works which a quick 
google run will point you to.

good luck,

f.




More information about the Python-list mailing list