Making recursions faster, 7 million times…

The title of the post is not misguiding at all. Take a look at the time-taken numbers for the two recursion codes, and the huge difference in them –

Time taken by conventional recursion = 720000000µs (12 mins)
Time taken by improved recursion = 92µs…


This content originally appeared on DEV Community and was authored by Manish

The title of the post is not misguiding at all. Take a look at the time-taken numbers for the two recursion codes, and the huge difference in them -

Time taken by conventional recursion = 720000000µs (12 mins)
Time taken by improved recursion = 92µs
Faster by = 720000000/92
Enter fullscreen mode Exit fullscreen mode

The 'improved' recursion code is faster by well over 7 million times. Read on to understand how it is done.

Most of the programmers are familiar with recursive code, and almost all of us have solved recursive problems like Fibonacci series or Factorial during our CS courses. We know that if try higher numbers, the code crashes with StackOverflowError.

Dynamic programming or DP as it is popularly known is a blessing for many problems requiring either recursive or iterative algorithms. In short, dynamic programming is an approach of solving complex problems by breaking them into several smaller sub-problems, where the sub-problems are overlapping sub-problems. It can make lot of recursive problems much more efficient. Dynamic programming approach is similar to recursive programming, however in dynamic programming the intermediate results are cached/stored for future calls.

If we consider recursive programing for Fibonacci series, computing the nth number depends on the previous n-1 numbers and each call results in two recursive calls. Thus, the time complexity is –

T(n) = T(n-1) + T(n-2) = O(2ⁿ)

In other words, as we increase the Fibonacci number, the time taken to compute that Fibonacci number increases exponentially. On the other hand, if we use Dynamic programming for the Fibonacci number, since the earlier results are already cached, we simply have complexity as –

Time Complexity = O(n)
Extra Space = O(n)

In fact, it is fairly easy to reduce additional space for Fibonacci number by just storing previous two results instead of storing all the previous results.

It means that Dynamic programming can drastically reduce the time taken to compute solutions that require several recursive/iterative calls. I've written this small piece of Python code that computes Fibonacci number using recursion as well as Dynamic programming. See the execution results posted below the code to know respective time taken while computing the 45th Fibonacci number. You can try this code with different numbers on your own computer as well, and see how you can make your recursions million times faster.

from timeit import default_timer as timer
from datetime import timedelta

fibo_cache = [0]*200

def recursive_fibonacci(num):
    if num in (0, 1):
        return num

    return recursive_fibonacci(num-1) + recursive_fibonacci(num-2)


def dynamic_fibonacci(num):
    if num in (0, 1):
        return num
    elif 0 != fibo_cache[num]:
        return fibo_cache[num]

    fibo_cache[num] = dynamic_fibonacci(num-1) + dynamic_fibonacci(num-2)
    return fibo_cache[num]


if __name__ == '__main__':

    fibo_num = 45  # change as needed
    start = timer()
    print(f"Dynamic fibonacci answer = {dynamic_fibonacci(fibo_num)}")
    dyna_end = timer()
    print(f"Time for Dynamic = {timedelta(seconds=dyna_end-start)}")
    print(f"Recursive fibonacci answer = {recursive_fibonacci(fibo_num)}")
    rec_end = timer()
    print(f"Time for Recursive = {timedelta(seconds=rec_end-start)}")
Enter fullscreen mode Exit fullscreen mode

Recursion using dynamic programmig

This code is hosted on GitHub here - fibo.py

Cover image gratitude: http://xkcdsw.com/1105


This content originally appeared on DEV Community and was authored by Manish


Print Share Comment Cite Upload Translate Updates
APA

Manish | Sciencx (2021-02-22T12:31:13+00:00) Making recursions faster, 7 million times…. Retrieved from https://www.scien.cx/2021/02/22/making-recursions-faster-7-million-times/

MLA
" » Making recursions faster, 7 million times…." Manish | Sciencx - Monday February 22, 2021, https://www.scien.cx/2021/02/22/making-recursions-faster-7-million-times/
HARVARD
Manish | Sciencx Monday February 22, 2021 » Making recursions faster, 7 million times…., viewed ,<https://www.scien.cx/2021/02/22/making-recursions-faster-7-million-times/>
VANCOUVER
Manish | Sciencx - » Making recursions faster, 7 million times…. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/02/22/making-recursions-faster-7-million-times/
CHICAGO
" » Making recursions faster, 7 million times…." Manish | Sciencx - Accessed . https://www.scien.cx/2021/02/22/making-recursions-faster-7-million-times/
IEEE
" » Making recursions faster, 7 million times…." Manish | Sciencx [Online]. Available: https://www.scien.cx/2021/02/22/making-recursions-faster-7-million-times/. [Accessed: ]
rf:citation
» Making recursions faster, 7 million times… | Manish | Sciencx | https://www.scien.cx/2021/02/22/making-recursions-faster-7-million-times/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.