Quick actions

cmd+k|ctrl+k

Navigation

Languages

Decorator 2_2024_10_07

Snippet info

Language

Python

Visibility

public

Author

aco

Created

2024-10-07T01:26:06.805805Z

Updated

2024-10-07T06:06:03.605281Z

def timed(fn):
    from time import perf_counter
    from functools import wraps
    @wraps(fn)
    def inner(*args, **kwargs):
        start = perf_counter()
        result = fn(*args, **kwargs)
        end = perf_counter()
        elapsed = end - start
        args_str = ', '.join([str(a) for a in args] + [f"{k}={v}" for k, v in kwargs.items()])
        print(f'{fn.__name__}({args_str}) took {elapsed:.6f}s to run.')
        return result
    return inner
    
def calc_recursive_fib(n):
    if n<= 2:
        return 1
    else:
        return calc_recursive_fib(n-1) + calc_recursive_fib(n-2)
        
calc_recursive_fib(3)

@timed
def fib_recursed(n):
    return calc_recursive_fib(n)
    
fib_recursed(33)
        
INFO