Last active
September 26, 2024 16:03
-
-
Save goutomroy/f293c203022987570e77cd9e199858c8 to your computer and use it in GitHub Desktop.
A Python decorator which will calculate function execution time in seconds and prints it in console.
This file contains hidden or 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): | |
@functools.wraps(func) | |
def wrapper(*args, **kwargs): | |
start_time = time.perf_counter() | |
value = func(*args, **kwargs) | |
end_time = time.perf_counter() | |
run_time = end_time - start_time | |
print("Finished {} in {} secs".format(repr(func.__name__), round(run_time, 3))) | |
return value | |
return wrapper |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment