Last active
November 22, 2022 03:09
-
-
Save yipo/ab398242cefc70bf59251607faf5d914 to your computer and use it in GitHub Desktop.
Calculate the time cost
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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