I am surprised that the iterative approach is less efficient. Typically, the recursive approach will be slower due to the overhead of method calls. If your algorithm can be implemented as tail-recursive, it will almost certainly be faster as an iterative implementation. Can you tell us more about what you are actually trying to do? Perhaps the performance difference is more algorithmic than just switching iterations for recursion. Here is an example from some CS lecture notes that refer to a recursive approach to calculating Fibonacci numbers, which is O (2 ^ n), while the iterative approach To). I believe (although I have not tried it) you can write a recursive Fibonacci number generator, which is O (n).
Edit:
Last thought. IMHO it would be much better to use a slower problem-free approach than introduce all the complexity in trying to determine that you are going to overflow the stack and have some kind of backup mechanism to avoid it.
sceaj source share