python 简单类装饰器:时间统计
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
import time
from functools import wraps
def time_statistics(func):
@wraps(func)
def inner_func(*args, **kwargs):
st = time.time()
result = func(*args, **kwargs)
consume = time.time() - st
if consume > 3:
print('超过3秒', func.__name__, consume)
return result
return inner_func
class Calculate(object):
def __init__(self):
pass
@time_statistics
def add(self, x, y):
time.sleep(4)
return x + y
if __name__ == '__main__':
Calculate().add(1, 2)

浙公网安备 33010602011771号