Today I found a short yet interesting article on codeproject (which is the place to be if your interested in the latest C# developments) on recursion versus iteration. It explains that it should be common practice to use iterative methods instead of recursive, especially because of possible stack overflows. If you can’t avoid recursion, the article also explains a technique called “memoization” (without the ‘r’ :-) ). This is basically a key-value collection (such as a generic dictionary) that remembers the result of a previous function call so the output doesn’t need to be calculated again.

An excerpt:

N Recursive Iterative
10 334 ticks 11 ticks
100 846 ticks 23 ticks
1000 3368 ticks 110 ticks
10000 9990 ticks 975 ticks
100000 stack overflow 9767 ticks

The full article by Eyal Lantzman is available here

Trackback URI | Comments RSS

Leave a Reply