Skip to content

Instantly share code, notes, and snippets.

@griiid
Forked from yipo/time_cost.py
Created November 22, 2022 03:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save griiid/93e95f81dcda8ce3a44927850fd359d4 to your computer and use it in GitHub Desktop.
Save griiid/93e95f81dcda8ce3a44927850fd359d4 to your computer and use it in GitHub Desktop.
Calculate the time cost
#!/usr/bin/env python
import time
from contextlib import contextmanager
from datetime import timedelta
@contextmanager
def time_cost():
begin = time.time()
try:
yield
finally:
print('time cost:', timedelta(seconds=time.time() - begin))
if __name__ == '__main__':
with time_cost():
sum(range(1000000))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment