Thread: Micro-optimizing Fibonacci n-th term generator: a journey to perfection

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Mindaugas View Post
    Therefore I will stick with microptimisations for the time being (hence the name of the thread ) this means that the algorithm will be recursive.
    You don't seem to have any idea how bad the exponential time taken by the naive recursive approach is.
    Doubly-recursively calculating the Fib of N+1 takes more than twice the effort of calculating the Fib of N.
    It's so bad that we're talking over a billion times slower to calculate even Fib(50) recursive rather than iteratively. Should you want to go to say Fib(100), it takes so long that you would literally grow old and die before it finished.
    But if done using a simple loop with two variables, one can easily calculate Fib(100) in less than a millisecond.

    Knowing that, I will also tell you that an iterative loop can be translated to a simple form of single-recursion, sort of the opposite of doing the tail-recursion elimination.
    Tail recursion elimination is a useful skill to learn, but going the other way - not so much.
    Last edited by iMalc; 12-31-2014 at 03:16 PM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Lagged Fibonacci generator
    By drew99 in forum General Discussions
    Replies: 3
    Last Post: 10-21-2012, 07:47 AM
  2. Looking for source code of Lagged Fibonacci generator
    By user_name_void in forum C Programming
    Replies: 5
    Last Post: 02-28-2010, 02:29 PM
  3. A journey to nowhere
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 03-31-2007, 11:55 AM
  4. Magneto's journey to becoming a C++ expert.
    By Magneto in forum C++ Programming
    Replies: 21
    Last Post: 07-17-2005, 08:16 PM
  5. Fibonacci How should I make a generator
    By cprog in forum C Programming
    Replies: 12
    Last Post: 10-06-2002, 04:04 PM

Tags for this Thread