DEV Community

Cover image for Increase the speed of your program by 1000000 times 🤯 by using this technique!
Aahnik Daw
Aahnik Daw

Posted on Edited on

Increase the speed of your program by 1000000 times 🤯 by using this technique!

Open a jupyter notebook to follow along. The following code blocks are cells in an ipynb notebook.

Pre-requisites:

You must know how to find the n-th term of the Fibonacci sequence using recursion.

Fibonacci

Link to video.

image

from timeit import timeit
📺 Info Siaran: aahnik/increase the speed of your program by times by using this technique ma - Info Live Streaming 2026 📺 Info Siaran: aahnik/increase the speed of your program by times by using this technique ma - Info Live Streaming 2026

Now lets first make the standard recursive function.

image

def fibo(n: int) -> int:
    # without memoization
    if n <= 2:
        return 1
    return fibo(n-1) + fibo(n-2)
📺 Info Siaran: aahnik/increase the speed of your program by times by using this technique ma - Info Live Streaming 2026 📺 Info Siaran: aahnik/increase the speed of your program by times by using this technique ma - Info Live Streaming 2026

Now let's try to use memoization, that will reduce the time complexity to n.

image

def memo_fibo(n: int, memo: dict = {}) -> int:
    # with memoization
    if n in memo:
        return memo[n]
    if n <= 2:
        return 1
    memo[n] = memo_fibo(n-1, memo) + memo_fibo(n-2, memo)
    return memo[n]
📺 Info Siaran: aahnik/increase the speed of your program by times by using this technique ma - Info Live Streaming 2026 📺 Info Siaran: aahnik/increase the speed of your program by times by using this technique ma - Info Live Streaming 2026

Let us set a value of n (the term of the Fibonacci sequence we want to find )

n = 40
📺 Info Siaran: aahnik/increase the speed of your program by times by using this technique ma - Info Live Streaming 2026 📺 Info Siaran: aahnik/increase the speed of your program by times by using this technique ma - Info Live Streaming 2026

Let us execute the normal way. (Here I have used a magic command of Jupyter Notebook, to find the execution time)

%%timeit -n 1 -r 10
fibo(n)
📺 Info Siaran: aahnik/increase the speed of your program by times by using this technique ma - Info Live Streaming 2026 📺 Info Siaran: aahnik/increase the speed of your program by times by using this technique ma - Info Live Streaming 2026
33.4 s ± 257 ms per loop (mean ± std. dev. of 10 runs, 1 loop each)
📺 Info Siaran: aahnik/increase the speed of your program by times by using this technique ma - Info Live Streaming 2026 📺 Info Siaran: aahnik/increase the speed of your program by times by using this technique ma - Info Live Streaming 2026

Now let us execute the function that used the memo.

%%timeit -n 1 -r 10
memo_fibo(n)
📺 Info Siaran: aahnik/increase the speed of your program by times by using this technique ma - Info Live Streaming 2026 📺 Info Siaran: aahnik/increase the speed of your program by times by using this technique ma - Info Live Streaming 2026
The slowest run took 86.52 times longer than the fastest. This could mean that an intermediate result is being cached.
3.6 µs ± 9.59 µs per loop (mean ± std. dev. of 10 runs, 1 loop each)
📺 Info Siaran: aahnik/increase the speed of your program by times by using this technique ma - Info Live Streaming 2026 📺 Info Siaran: aahnik/increase the speed of your program by times by using this technique ma - Info Live Streaming 2026

The difference is stark. From 33.4 s to 3.6 µs.

Thus you see, how using memoization brings in a huge difference in time of execution.

The name 'Fibonacci' is due to a 13th-century Italian mathematician Leonardo of Pisa, who later came to be known as Fibonacci.

However, what we popularly call 'Fibonacci numbers' find their earliest mention in the 2nd century BCE work of Indian thinker Acharya Pingala.

Top comments (1)

📺 Info Siaran: aahnik/increase the speed of your program by times by using this technique ma - Info Live Streaming 2026
 
hentaichan profile image
ヘンタイちゃん •

or use the closed-form expression of the fibonacci sequence for constant time complexity