Skip to content

Instantly share code, notes, and snippets.

@goutomroy
Last active October 9, 2023 22:46
Show Gist options
  • Save goutomroy/f293c203022987570e77cd9e199858c8 to your computer and use it in GitHub Desktop.
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.
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