Skip to content

Instantly share code, notes, and snippets.

@yipo
Last active November 22, 2022 03:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save yipo/ab398242cefc70bf59251607faf5d914 to your computer and use it in GitHub Desktop.
Save yipo/ab398242cefc70bf59251607faf5d914 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