Skip to content

Instantly share code, notes, and snippets.

@qnkhuat
Last active October 20, 2022 03:13
Show Gist options
  • Save qnkhuat/d6770ac6eb199fee318e4c59b6fa9c1a to your computer and use it in GitHub Desktop.
Save qnkhuat/d6770ac6eb199fee318e4c59b6fa9c1a to your computer and use it in GitHub Desktop.
Timing processes in python with context manager
from contextlib import contextmanager
import time
@contextmanager
def timing(description: str = '') -> None:
start = time.time()
yield
print(f"Elasped {description}: {time.time() - start}")
### Usage
with timing("A simple call"):
# function_call(var)
print("Hello universe")
### Output
# Hello Uinverse
# Elasped A simple call : 0.000001s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment