美文网首页python学习
python 计算函数执行时间装饰器(装饰器写法)

python 计算函数执行时间装饰器(装饰器写法)

作者: SkTj | 来源:发表于2019-07-31 15:23 被阅读11次

import time
import functools
def clock(func):
@functools.wraps(func)
def clocked(*args, *kwargs):
t0 = time.time()
result = func(
args, **kwargs)
elapsed = time.time() - t0
name = func.name
arg_lst = []
if args:
arg_lst.append(', '.join(repr(arg) for arg in args))
if kwargs:
pairs = ['%s=%r' % (k, w) for k, w in sorted(kwargs.items())]
arg_lst.append(', '.join(pairs))
arg_str = ', '.join(arg_lst)
print('[%0.8fs] %s(%s) -> %r ' % (elapsed, name, arg_str, result))
return result
return clocked

@clock
def test():
sum=0
for i in range(1000):
sum+=i
print(sum)

test()

property,classmethod,staticmethod

相关文章

  • python 计算函数执行时间装饰器(装饰器写法)

    import timeimport functoolsdef clock(func):@functools.wra...

  • Python装饰器

    Python装饰器 一、函数装饰器 1.无参装饰器 示例:日志记录装饰器 2.带参装饰器 示例: 二、类装饰器 示例:

  • 2019-05-26python装饰器到底是什么?

    装饰器例子 参考语法 装饰器是什么?个人理解,装饰器,是python中一种写法的定义。他仍然符合python的基本...

  • Flask 视图和路由的进阶技能

    视图装饰器 Python 装饰器是用于转换其它函数的函数。当一个装饰的函数被调用的时候,装饰器也会被调用。接着装饰...

  • 解惑,从新认识python装饰器

    概念 python有两种装饰器: 函数装饰器(function decorators) 类装饰器(class de...

  • python 装饰器 补充

    重新理解python 装饰器 python 装饰器是一个函数,被装饰器所装饰的代码块最终也是一个函数这个对于一般的...

  • 装饰器详解

    装饰器基本写法: 高级一点的装饰器 带参数的装饰器和类装饰器属于进阶的内容。在理解这些装饰器之前,最好对函数的闭包...

  • Python装饰器

    Python装饰器 装饰器的本质是什么? 装饰器等价于高阶函数,形如myfunc=decorator(myfunc...

  • python装饰器

    装饰器简述 要理解装饰器需要知道Python高阶函数和python闭包,Python高阶函数可以接受函数作为参数,...

  • 第012篇:三大神器之装饰器

    Python的三大神器:装饰器、迭代器、生成器 1、装饰器  1.1、什么是装饰器 装饰器的本质是一个函数 = 返...

网友评论

    本文标题:python 计算函数执行时间装饰器(装饰器写法)

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