Sunday, August 23, 2009

Recursion vs iteration

Situations not to use recursion:
- recomputation of values (fibonacci, factorial, GCD)
- exhaust memory very quickly

Advisable to use iteration or memoization, instead, if you are so keen and persistent to implement recursion.

1 comment:

  1. I really like recursion. Some algorithms can be really more elegant using it. Most problems that arise are really simple to fix. Like memory exhaustion. Unless you are dealing with mobile applications, spending some Kb on the stack for more method calls shouldn't bother. Of course, passing arrays by copy (or big structs - on C) would exhaust your memory. But if you use pointers (or references) it is no big deal.

    ReplyDelete