美文网首页
python装饰器统计各模块耗时

python装饰器统计各模块耗时

作者: Han筱希 | 来源:发表于2019-05-22 22:21 被阅读0次

此方法不改变被装饰函数的返回值

import datetime
import functools
import time

def time_me(func):
    '''
    @summary: cal the time of the fucntion
    @param : None
    @return: return the res of the func
    '''
    def wrapper(*args,**kw):
        start_time = datetime.datetime.now()
        res = func(*args,**kw)
        over_time = datetime.datetime.now()
        print ('current Function {0} run time is {1}'.format(func.__name__ , (over_time - start_time).total_seconds()))
        return res
    return wrapper

@time_me
def test1():
    time.sleep(1)
    return 'aaa'

A = test1()
print A

执行结果:

current Function test1 run time is 1.00057
aaa

相关文章

  • python装饰器统计各模块耗时

    此方法不改变被装饰函数的返回值 执行结果:

  • lru_cache装饰器的作用

    python lru_cache装饰器的作用 ru_cache装饰器实现了备忘功能,能够优化函数执行速度,他把耗时...

  • python 装饰器的使用详解

    注意事项 何时执行装饰器函数装饰器在导入模块时立即执行,而被装饰的函数只在明确调用时运行。这突出了 Python ...

  • python-面试QA

    语言 讲讲日常开发中都用到了那些Python内置的模块 推荐一本看过较好的python书籍? 装饰器、迭代器、ye...

  • 编程学习-Python每天一点代码

    1.python的基本知识 (1)模块:request/bs4 (2)库:BeautifulSoup (3)装饰器...

  • 装饰器模式

    介绍 在python装饰器学习 这篇文章中,介绍了python 中的装饰器,python内置了对装饰器的支持。面向...

  • python中的装饰器

    python装饰器详解 Python装饰器学习(九步入门) 装饰器(decorator) 就是一个包装机(wrap...

  • [译] Python装饰器Part II:装饰器参数

    这是Python装饰器讲解的第二部分,上一篇:Python装饰器Part I:装饰器简介 回顾:不带参数的装饰器 ...

  • Python中的装饰器

    Python中的装饰器 不带参数的装饰器 带参数的装饰器 类装饰器 functools.wraps 使用装饰器极大...

  • Python进阶——面向对象

    1. Python中的@property   @property是python自带的装饰器,装饰器(decorat...

网友评论

      本文标题:python装饰器统计各模块耗时

      本文链接:https://www.haomeiwen.com/subject/ckipzqtx.html