Created
March 14, 2022 22:22
-
-
Save CEZERT/3c7f28f60ba082e5de03950f123647cf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import functools | |
import time | |
def timer(func): | |
"""Print the runtime of the decorated function""" | |
@functools.wraps(func) | |
def wrapper_timer(*args, **kwargs): | |
start_time = time.perf_counter() # 1 | |
value = func(*args, **kwargs) | |
end_time = time.perf_counter() # 2 | |
run_time = end_time - start_time # 3 | |
print(f"Finished {func.__name__!r} in {run_time:.4f} secs") | |
return value | |
return wrapper_timer | |
@timer | |
def waste_some_time(num_times): | |
for _ in range(num_times): | |
sum([i**2 for i in range(10000)]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment