Python3.6.3
import time
__all__ = ['print_time']
def print_time(f):
"""Decorator of viewing function runtime.
eg:
```py
from print_time import print_time as pt
@pt
def work(...):
print('work is running')
word()
# work is running
# --> RUN TIME: <work> : 2.8371810913085938e-05
```
"""
def fi(*args, **kwargs):
s = time.time()
res = f(*args, **kwargs)
print('--> RUN TIME: <%s> : %s' % (f.__name__, time.time() - s))
return res
return fi
# test
@print_time
def _test1():
print('work is running')
if __name__ == '__main__':
_test1()
放到Lib里面设为只读方便使用