Skip to content

Instantly share code, notes, and snippets.

@DahlitzFlorian
Last active April 23, 2019 19:38
Show Gist options
  • Save DahlitzFlorian/5e2ff136af9c43b1f8036b79d135d340 to your computer and use it in GitHub Desktop.
Save DahlitzFlorian/5e2ff136af9c43b1f8036b79d135d340 to your computer and use it in GitHub Desktop.
Article: How To Create Your Own Timing Context Manager
from contextlib import contextmanager
from time import time
@contextmanager
def timing(description: str) -> None:
start = time()
yield
ellapsed_time = time() - start
print(f"{description}: {ellapsed_time}")
with timing("List Comprehension Example"):
s = [x for x in range(10_000_000)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment