Loop vs. Recursion

Definition

Saperate big problem into small repeated parts and solve the problem.

All Recursion can be converted to Loop.

Good and bad points

LoopRecursion
intuitivecomplicated
Long codesshort codes
good readabilitybad readability
use less stack memoriesuse much stack memories
very low possiblity to get Stack overflowStack overflow if too mcuh call
very low possiblity to get overhead issueoverhead issue: call function repeatly will make program slow
each level of variable state will not savedeach level of variable state will saved because of stack

Actual code

We should write recursion because it is good for reading and fixing it.

However, we need to use loop when..

  • input may cause stack overflow : If the program need to calculate huge number of Fibonacci sequence
  • the program need to be fast : call loop function 10 million times in 1 second.

 Share!

 
comments powered by Disqus