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)

  

 

posted @ 2020-12-11 16:37  Adamanter  阅读(207)  评论(0)    收藏  举报