Coder Profile - Show off your skills, get a coder profile.
 
 
 
The Author
Cinjection
Oleksi Derkatch
Send A Message
Rating
8.00
out of 10
( 2 Ratings )
Please login to rate source codes.

Click here to register a free account with us.
General Details
C++
Posted 4.3 Years Ago
689 Views
Received 2 Ratings
More Codes By This Author
Wordsearch generator.
Linked List in C#
Julia Set
Mandelbrot Set Generator
MD5 Brute-Forcer
Articles By This Author
Intorduction to memoizati...
Strings in C++
Vectors in C++
Use Cases
[C++] Pointers and their...

Contrasting Recursion, Iteration, and memoization for Fibonacci's Sequence


Description
Here is some code that I made to contrast the execution time of a couple differently styled methods. All of them all recursive procedures, but some of the functions produce a iterative process (namely, the tail-recursive ones).

Each function calculates a term of Fibonacci's sequence. Each functions is timed to the time it takes to output the first 1400 terms of this sequence.

fibonacci(int n);
This one combines the simplest solution, using tree recursion, but I include memoization, a dynamic programming technique, to see how much more efficient that will make it.

fib2(double n1, double n2, int terms);
This is the plain tree recursive version. The simplest to code and understand, but hugely inefficient.

fib3(double n1, double n2, int terms);
This is the plain tail-recursive version. It generates and iterative process.

fib4(double n1, double n2, int terms)
This version applies the memoization technique to the tail-recursive version

Here are the results:

Memoization + Recursion: 1.562 seconds
Memoization + Iteration: 1.5 seconds
Iteration: 1.453 seconds
Recursion: much too long to time.

I'm curious as too why memoization actually slows down the iterative approach. If anyone knows, please let me know :) So what do you think of my little experiment :)
Source Code
Comments
Please login to post comments.
 
ephekt     Posted 2.15 Years Ago
 
 
Hi,

The reason why you are not seeing a difference in performance is definitely related
to the issue that you are always doing recursive computation. fib3 is a recursive
function.

Another issue you might be facing is the way you define your static hash. I would
define it as a global variable outside of any of the functions. For the values up to
1,400 for Fib you should really see a performance increase using dynamic programming.
 
Cinjection     Posted 4.1 Years Ago
 
 
lol. I'm glad you enjoyed it. I didn't get what you meant with iostream
though.
 
Wiidude     Posted 4.1 Years Ago
 
 
also, it can do my math homework :)
 
Wiidude     Posted 4.1 Years Ago
 
 
Yeah, i copied that into DEVC++, and after it is done, type <iostream> makes it
do alot more, complex calculations.
Page 1 of 1
More "C++" Source Codes By This Author
Recently Posted "C++" Source Codes
Recently Rated "C++" Source Codes
 
 
Latest News About Coder Profile
Coder Profile Poll
Why do you get bored with programming?

Not enough time to do something productive
I run out of ideas
Too hard to show people my creations
Everything i do has too many errors, and it's too hard
I don't get bored!!!


please login to cast your vote
and see the results of this poll
Latest Coder Profile Changes
Coder Profile was last updated
3.20 Years Ago
Official Blog :: Make A Donation :: Credits :: Contact Me
Terms & Conditions :: Privacy Policy :: Documents :: Wallpapers
Version 1.46.00
Copyright © 2007 - 2012, Scott Thompson, All Rights Reserved