import datetime
def print_calc_time(func):
def wrapper(*args, **kw):
start_time = datetime.datetime.now()
func(*args, **kw)
end_time = datetime.datetime.now()
ss = end_time - start_time
print('Function <{}> run time is {}s.'.format(func.__name__, ss))
return wrapper
@ print_calc_time
def hello():
time.sleep(1.5)
print('hi~')
hello()
输出:
hi~
Function run time is 0:00:01.501977s.