+ 4
I find it hard to say. Any recursive function, if not tail-recursive, entails an overhead through the function calls and may be implemented more efficiently in an iterative fashion.
On the other hand, some algorithms and problems are intrinsically recursive and can be solved iteratively only with cost of clarity, readability, and shortness.
It is not uncommon that gain in one area (e.g. speed of execution) entails cost in a different area (maintainability, security, provability, modifiability, ...) It would be short-sighted to compare two versions of one algorithm only on the basis of a single quality attribute.
Asides of that, the Ackermann function is a function that, I believe, is not easily written iteratively, and the recursive version may indeed be more efficient. But that is a rather academic scenario.
Also mind that a recursive implementation of the Fibonacci sequence can be bad O(2^n) or good O(n). Don't compare a good iterative version with a bad recursive one.
+ 4
Memoisation. You can use memoisation also with iterative algorithms.
But I don't think you should focus too much on whether or not one is a few microseconds faster than the other. Time consumption also matters on how often the CPU has the switch context in preemptive multitasking for example, or the possibility to prefetch memory, and optimisation of loops on the fly, and efficiency of branch predictions...
More important to understand is the concept of asymptotic time and memory behaviour, and to be able to improve those. Microoptimisations are very rare and at best used in hot loops of time critical portions of the code.
+ 1
belal bayrakdar for factorial problem also we use recursion
+ 1
belal bayrakdar ,
Maybe but we usually use recursion for factorial
+ 1
You're welcome :)
+ 1
<DD status="busy"> I got it. thanks