Skip to main content

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

RecursiveNon-recursive (iterative)
Function calls itselfUses loops
Needs base caseNeeds a loop condition
More memory (stack frames)Less memory
Slower due to call overheadFaster
Short, easy to write for naturally recursive problems (TOH, tree traversal)Longer or harder for such problems
Risk of stack overflowNo such risk

✓ Answer checked by Claude Opus 5.5 (high effort)

All Data Structure and Algorithm chapter 5 questions →