4 ms·
If you're getting a stack overflow, you might check if your language supports tail call optimization, or just known as tail recursion. Not all environments supp
by imglorp 7y ago
If you're getting a stack overflow, you might check if your language supports tail call optimization, or just known as tail recursion. Not all environments support this, but if you can rearrange your recursive function it would avoid the overflow.
The short version of the tail call story is only one copy of the stack frame is kept, instead of all of the frames on the way down.
- iCodeSometime 7y agoAlternatively, adding some kind of memoization can reduce run time and stack size pretty significantly for some algorithms. e.g. if you're using recursion to compute fibonacci, you're re-computing quite a lot of steps by default