decorator2 - 7 oct 2024

Run Settings
LanguagePython
Language Version
Run Command
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) # Corrected from *aargs end = perf_counter() elapsed = end - start args_str = [str(a) for a in args] # Renamed to args_str kwargs_str = ['{0}={1}'.format(k, v) for k, v in kwargs.items()] # Fixed formatting all_args = args_str + kwargs_str # Corrected print statement print('{0}({1}) took {2:.6f}s to run.'.format(fn.__name__, ', '.join(all_args), elapsed)) 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) # Test the function # print(calc_recursive_fib(4)) @timed #timed only change the charateritics of function fib_recursed, but not calc_recursive_fib def fib_recursed(n): return calc_recursive_fib(n) fib_recursed(4) #we don't want to change the function calc_recursive_fib, so use fib_recursed as a sieve
Editor Settings
Theme
Key bindings
Full width
Lines