Skip to content

Instantly share code, notes, and snippets.

@hungyiwu
Forked from DahlitzFlorian/timer_class.py
Created July 15, 2020 02:38
Show Gist options
  • Save hungyiwu/b21b8f676aa9d33b0c7c9daa903a4ca1 to your computer and use it in GitHub Desktop.
Save hungyiwu/b21b8f676aa9d33b0c7c9daa903a4ca1 to your computer and use it in GitHub Desktop.
Article: How To Create Your Own Timing Context Manager
from time import time
class Timer(object):
def __init__(self, description):
self.description = description
def __enter__(self):
self.start = time()
def __exit__(self, type, value, traceback):
self.end = time()
print(f"{self.description}: {self.end - self.start}")
with Timer("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