Decorator for recursive functions

PHOTO EMBED

Tue Apr 12 2022 11:49:29 GMT+0000 (Coordinated Universal Time)

Saved by @taha #python

import time
from functools import lru_cache


def calculate_time(func):
    def wrapper(*args, **kwargs):
        start_time = time.time()
        result = func(*args, **kwargs)
        end_time = time.time()
        print(end_time - start_time)
        return result

    return wrapper


def fib(n):
    if n == 0 or n == 1:
        return 1

    return fib(n - 1) + fib(n - 2)


@calculate_time
@lru_cache
def call_fib(n):
    return fib(n)


print(call_fib(36))
print(call_fib(36))
content_copyCOPY