Click here to Skip to main content
11,935,066 members (49,804 online)
Click here to Skip to main content
Add your own
alternative version


26 bookmarked

Iterative vs. Recursive Approaches

, 5 Nov 2007 CPOL
Rate this:
Please Sign up or sign in to vote.
Implication of not thinking of iterative solutions over recursive from performance (response time) point of view


This article was originally posted at

Recursive function – is a function that is partially defined by itself and consists of some simple case with a known answer. Example: Fibonacci number sequence, factorial function, quick sort and more.
Some of the algorithms/functions can be represented in an iterative way and some may not.

Iterative functions – are loop based imperative repetitions of a process (in contrast to recursion which has a more declarative approach).

Comparison between Iterative and Recursive Approaches from Performance Considerations


//recursive function calculates n!
static int FactorialRecursive(int n)
    if (n <= 1) return 1;
    return n * FactorialRecursive(n - 1);

//iterative function calculates n!
static int FactorialIterative(int n)
    int sum = 1;
    if (n <= 1) return sum;
    while (n > 1)
        sum *= n;
    return sum;
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

As we can clearly see, the recursive is a lot slower than the iterative (considerably) and limiting (stackoverflow).

The reason for the poor performance is heavy push-pop of the registers in the ill level of each recursive call.


//--------------- iterative version ---------------------    
static int FibonacciIterative(int n)
    if (n == 0) return 0;
    if (n == 1) return 1;
    int prevPrev = 0;
    int prev = 1;
    int result = 0;
    for (int i = 2; i <= n; i++)
        result = prev + prevPrev;
        prevPrev = prev;
        prev = result;
    return result;
//--------------- naive recursive version --------------------- 
static int FibonacciRecursive(int n)
    if (n == 0) return 0;
    if (n == 1) return 1;
    return FibonacciRecursive(n - 1) + FibonacciRecursive(n - 2);
//--------------- optimized recursive version ---------------------
static Dictionary<int> resultHistory = new Dictionary<int>();

static int FibonacciRecursiveOpt(int n)
    if (n == 0) return 0;
    if (n == 1) return 1;
    if (resultHistory.ContainsKey(n)) 
        return resultHistory[n];

    int result = FibonacciRecursiveOpt(n - 1) + FibonacciRecursiveOpt(n - 2);
    resultHistory[n] = result;
    return result;
N Recursive Recursive opt. Iterative
5 5 ticks 22 ticks 9 ticks
10 36 ticks 49 ticks 10 ticks
20 2315 ticks 61 ticks 10 ticks
30 180254 ticks 65 ticks 10 ticks
100 too long/stack overflow 158 ticks 11 ticks
1000 too long/stack overflow 1470 ticks 27 ticks
10000 too long/stack overflow 13873 ticks 190 ticks
100000 too long/stack overflow too long/stack overflow 3952 ticks

As before, the recursive approach is worse than iterative however, we could apply memorization pattern (saving previous results in dictionary for quick key based access), although this pattern isn't a match for the iterative approach (but definitely an improvement over the simple recursion).


  1. The calculations may be wrong in big numbers, however the algorithms should be correct.
  2. For timer calculations, I used System.Diagnostics.Stopwatch.

Points of Interest

  1. Try not to use recursion in system critical locations.
  2. Elegant solutions not always the best performing when used in "recursive situations".
  3. If you required to use recursion, at least try to optimize it with dynamic programming approaches (such as memorization).


  • Post: November 06, 2007


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Eyal Lantzman
Software Developer (Senior) Taldor
Israel Israel
In the last couple of years I'm working as Microsoft sub contractor in various project types - LOB, applications, CnC applications and Distributed applicaitons all of them considered to be extra large in tems of man power (or brain power), duration and geographic destribution between connected sites.

You may also be interested in...

Comments and Discussions

QuestionNice Post Pin
Member 1120899119-Nov-14 7:58
memberMember 1120899119-Nov-14 7:58 
GeneralIt's a question of data structure Pin
virtuPIC15-Nov-07 21:45
membervirtuPIC15-Nov-07 21:45 
Generalblame C#, not .NET. F# does it better. Pin
Jay R. Wren13-Nov-07 3:00
memberJay R. Wren13-Nov-07 3:00 
GeneralRe: blame C#, not .NET. F# does it better. PinPopular
ncarey13-Nov-07 8:29
memberncarey13-Nov-07 8:29 
GeneralHum Pin
smesser6-Nov-07 6:06
membersmesser6-Nov-07 6:06 
I have a Core2 Duo and don't get anywhere near the performance you do.

What's your test system?
GeneralRe: Hum Pin
Eyal Lantzman6-Nov-07 21:08
memberEyal Lantzman6-Nov-07 21:08 
GeneralGood Job Pin
merlin9816-Nov-07 5:24
membermerlin9816-Nov-07 5:24 
GeneralGood Best Practice Pin
The DevMan6-Nov-07 1:33
memberThe DevMan6-Nov-07 1:33 
GeneralInteresting Pin
norm .net5-Nov-07 22:46
membernorm .net5-Nov-07 22:46 
GeneralRe: Interesting Pin
Eyal Lantzman5-Nov-07 23:32
memberEyal Lantzman5-Nov-07 23:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.151126.1 | Last Updated 6 Nov 2007
Article Copyright 2007 by Eyal Lantzman
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid