Function decorator displaying the function execution time. All kwargs are the arguments taken by the Timer class constructor.
(run_times=1, **timer_kwargs)
| 70 | |
| 71 | |
| 72 | def timeit(run_times=1, **timer_kwargs): |
| 73 | """Function decorator displaying the function execution time. |
| 74 | |
| 75 | All kwargs are the arguments taken by the Timer class constructor. |
| 76 | |
| 77 | """ |
| 78 | # store Timer kwargs in local variable so the namespace isn't polluted |
| 79 | # by different level args and kwargs |
| 80 | |
| 81 | def decorator(f): |
| 82 | def wrapper(*args, **kwargs): |
| 83 | timer_kwargs.update(print_at_exit=False) |
| 84 | with Timer(**timer_kwargs) as t: |
| 85 | for _ in range(run_times): |
| 86 | out = f(*args, **kwargs) |
| 87 | fmt = '[*] Execution time of function "%(function_name)s" for %(run_times)d runs is %(execution_time)s = %(execution_time_each)s * %(run_times)d [*]' |
| 88 | context = {'function_name': f.__name__, 'run_times': run_times, 'execution_time': t, 'execution_time_each': t.fmt(t.elapsed / run_times)[1]} |
| 89 | print(fmt % context) |
| 90 | return out |
| 91 | return wrapper |
| 92 | |
| 93 | return decorator |
| 94 | |
| 95 | |
| 96 | if __name__ == "__main__": |
nothing calls this directly
no outgoing calls
no test coverage detected