GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 21-Mar-2006, 15:53
earachefl earachefl is offline
Member
 
Join Date: Feb 2006
Posts: 178
earachefl is on a distinguished road

Performance comparison between recursive and iterative functions


While studying recursion, the classic example of the Fibonacci series was given. Just for giggles, I decided to do it as an iterative function for comparison. What an amazing performance difference! Really makes you wonder when a recursive function is actually useable. Comments?

Here's my code:
CPP / C++ / C Code:
#include <iostream>
using namespace std;

unsigned long fibonacci(unsigned long);

int main () 
{
   unsigned long result, number;
   
   for(number = 0; number <= 40; number++)
   {
      result = fibonacci(number);
      cout << "Fibonacci(" << number << ") = " << result << endl;
   }
   return 0;
}

//Recursive fibonacci function
/*unsigned long fibonacci(unsigned long n)
{
   if (n==0||n==1)
      return n;
   else
      return fibonacci(n-1) + fibonacci(n-2);
}*/

//Iterative fibonacci function
unsigned long fibonacci(unsigned long n)
{
   static unsigned long int f, f1, f2;
   if (n == 0)
   {
      f1 = 0;
	  f2 = 0;
   }
   else if (n == 1 || n == 2)
   {
      f1 = 1;
	  f2 = 0;
   }
   
   f = f1 + f2;
   f2 = f1;
   f1 = f;

   return f;
}
And here is the output:
Quote:
Fibonacci(0) = 0
Fibonacci(1) = 1
Fibonacci(2) = 1
Fibonacci(3) = 2
Fibonacci(4) = 3
Fibonacci(5) = 5
Fibonacci(6) = 8
Fibonacci(7) = 13
Fibonacci(8) = 21
Fibonacci(9) = 34
Fibonacci(10) = 55
Fibonacci(11) = 89
Fibonacci(12) = 144
Fibonacci(13) = 233
Fibonacci(14) = 377
Fibonacci(15) = 610
Fibonacci(16) = 987
Fibonacci(17) = 1597
Fibonacci(18) = 2584
Fibonacci(19) = 4181
Fibonacci(20) = 6765
Fibonacci(21) = 10946
Fibonacci(22) = 17711
Fibonacci(23) = 28657
Fibonacci(24) = 46368
Fibonacci(25) = 75025
Fibonacci(26) = 121393
Fibonacci(27) = 196418
Fibonacci(28) = 317811
Fibonacci(29) = 514229
Fibonacci(30) = 832040
Fibonacci(31) = 1346269
Fibonacci(32) = 2178309
Fibonacci(33) = 3524578
Fibonacci(34) = 5702887
Fibonacci(35) = 9227465
Fibonacci(36) = 14930352
Fibonacci(37) = 24157817
Fibonacci(38) = 39088169
Fibonacci(39) = 63245986
Fibonacci(40) = 102334155
  #2  
Old 21-Mar-2006, 16:17
QED's Avatar
QED QED is offline
Member
 
Join Date: Feb 2005
Location: Hudson Valley, NY
Posts: 231
QED is a jewel in the roughQED is a jewel in the roughQED is a jewel in the rough

Re: Performance comparison between recursive and iterative functions


Recursion often has other benefits that can make up for lost performance. In particular, the readability and elegance of the code, and maintainability.

If the body of the function is heavy, then the function-call overhead can quickly become insignificant. If the function body is small and light, as in your Fibonacci example, then the overhead for the recursive calls can be significant in comparison.

Matthew
__________________
I was born not knowing and have only had a little time to change that here and there. -- Richard P. Feynman

Boris Podolsky: James! How's the rat business?
James Moreland: Well, actually it's mostly students I'm experimenting on now.
Kurt Gödel: My God, the mazes must be enormous.
 
 

Recent GIDBlogWriting a book by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 09:04.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.