From: Andrew Poelstra on 8 Apr 2010 11:03 On 2010-04-08, Lie Ryan <lie.1296(a)gmail.com> wrote: > > Another approach which doesn't use recursion is iterating the array by > manually managing a "frame stack". With recursion, we're actually using > the function call stack as our "frame stack". However, unless your > language doesn't support recursion, I don't think there's any practical > advantage in using an iterative approach for iterating a recursive data > structure (e.g. variable depth array). > Sometimes when you have a lot of local variables and function parameters, and only a few are relevant to the recursive part, you get a significant speedup (and memory savings) by managing your own stack. Also, when you need a lot of stack space, you can use malloc() on a roll-your-own stack, which reports failure nicely and will also probably give you more memory before failing. Finally, it's a little easier to analyse a problem that does its own stack management because you can clearly see what is being pushed and popped, and when. -- Andrew Poelstra http://www.wpsoftware.net/andrew
First
|
Prev
|
Pages: 1 2 3 Prev: repeating elements in an array Next: The trim function and its intented purpose? |