- 2072 Asoj · 1+2 marks
Is recursion slow; recursive vs non-recursive
Short answer
Yes, recursion is generally slower than iteration because each call creates and destroys a stack frame, adding overhead. Some recursions (like naive Fibonaci) suffer from repeated subproblems, causing exponential time. However, for naturally recursive problems like tree traversal or Tower of Hanoi, recursion yields clearer code.
Do you think recursive function is slow? Compare recursive and non-recursive functions.
Is recursion slow?
Yes, generally a recursive function is slower than the equivalent loop, because every call needs time to create and destroy a stack frame (pass arguments, save return address) and uses extra stack memory. Some recursive algorithms (naive Fibonacci) are far slower because they repeat the same sub-problems: fib(30) makes 2,692,537 calls (counted by program) whereas a loop needs 30 steps.
Recursive vs non-recursive
| Recursive | Non-recursive (iterative) |
|---|---|
| Function calls itself | Uses loops |
| Needs base case | Needs a loop condition |
| More memory (stack frames) | Less memory |
| Slower due to call overhead | Faster |
| Short, easy to write for naturally recursive problems (TOH, tree traversal) | Longer or harder for such problems |
| Risk of stack overflow | No such risk |
✓ Answer checked by Claude Opus 5.5 (high effort)
Related questions
- Recursion; iterative vs recursive choice · Data Structure and Algorithm, chapter 5
- Normal vs recursive function · Data Structure and Algorithm, chapter 5
- Tail and non-tail recursion · Data Structure and Algorithm, chapter 5
- Define recursion; direct vs indirect · Data Structure and Algorithm, chapter 5
- Recursion, Fibonacci and TOH · Data Structure and Algorithm, chapter 5
- Recursion properties, Fibonacci, TOH algorithm · Data Structure and Algorithm, chapter 5
- Recursive function and how it works · Computer Programming, chapter 6 · asked 3×
- Overloading via member and non-member function · Object Oriented Programming, chapter 5
- Compare two strings with function · Computer Programming, chapter 7
- Unique vs non-unique address decoding · Instrumentation II, chapter 2