首页 > 编程语言 >python 时间戳装饰器

python 时间戳装饰器

时间:2022-08-21 12:22:44浏览次数:56  
标签:return python counter 时间 func time print 装饰 def

点击查看代码
import time
from functools import wraps

def timer(func):
    @wraps(func)
    def inner(*args, **kwargs):
        start = time.time()
        res = func(*args, **kwargs)
        end = time.time()
        print("{0}运行耗时: {1:.2f}".format(func.__name__, end - start))
        return res
    return inner


@timer
def counter(x):
    print("counter running!!!")
    time.sleep(3)
    return x


x = counter(100)
print(counter.__name__)
print(x)

标签:return,python,counter,时间,func,time,print,装饰,def
From: https://www.cnblogs.com/seven-lv/p/16609785.html

相关文章

  • 牛客网笔试输入输出处理方法总结(基于Python3.5)
    牛客网判题系统输入处理牛客网上的输入输出借鉴ACM模式给出,对于习惯了leetcode函数定义形式解题的小伙伴们来说确实比较生疏。为了避免在之后的笔试中再次吃亏,在这里对牛......
  • python文件上传
    前端代码:html<inputid="fileUpload"type="file"name="upload"><inputtype="button"@click="submitfile"value="Upload">jssubmitfile(){......
  • python wraps装饰器
    fromfunctoolsimportwrapsdefdecorator(func):"""thisisdecorator__doc__"""@wraps(func)defwrapper(*args,**kwargs):"""thisisw......
  • python输入和类型转换
    输入获取用户使用键盘录入的内容使用的函数是input()变量=input(‘提示的信息’)1.代码从上到下执行,遇到input函数之后 类型转换根据代码的需要,将一种数据类型......
  • 设计模式09 - 设计模式 - 装饰器模式(结构型)
    一、定义装饰器(Decorator)模式:指不改变现有对象结构的情况下,动态地给该对象增加额外功能。它是继承方式的一种替代方案。这种模式创建了一个装饰类,用来包装原有......
  • Notepad plus 通过NppExec插件编译/运行 golang,php,python等语言
        1. 在Notepadplus的插件-->插件管理中,添加nppExec插件。          2.打开插件-->NppExec,选择Showconsole,和Follow($CURRE......
  • jmeter-10-提取多个id拼接请求数据及日期时间偏移,你会了吗?
    前言平时在使用Jmeter过程中,可能会遇到各种需求的参数需要处理,比如提取id拼接数组,又如时间日期处理等等那么接下来将记录平时个人使用时遇到过挺多的场景!gogogo!一、......
  • python---re
    python---repython的re模块简单使用re.findall,re.compile,re.match和re.searchre.findall这个是最好用的,查找所有符合条件的,返回list,或Noneimportrestr1='gdf......
  • python---struct
    python---structkeywords:structbytespython数据互转https://docs.python.org/3/library/struct.html二进制数据和各种类型数据的转换因为不同平台默认大小端不同,......
  • Python custom modify the __add__ method All In One
    PythoncustommodifytheaddmethodAllInOnePython改写__add__类方法"""#classJuice:#def__init__(self,name,capacity):#self.na......