Skip to content

Instantly share code, notes, and snippets.

@otsaw
Created January 27, 2022 12:36
Show Gist options
  • Save otsaw/03f38c2b82eb3209e6a294a79ca8265e to your computer and use it in GitHub Desktop.
Save otsaw/03f38c2b82eb3209e6a294a79ca8265e to your computer and use it in GitHub Desktop.
Python context manager timer
import contextlib
import time
@contextlib.contextmanager
def Timer():
start = time.time()
yield lambda: time.time() - start
print(f"Elapsed: {time.time() - start:.3f} s")
with Timer():
do_something()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment