返回顶部

python 添加统计运行时间的功能[装饰器]

python 添加统计运行时间的功能[装饰器]

脚本一

没有修改被装饰对象的调用方式,但修改了源代码

#!/bin/python3
# -*- coding: utf-8 -*-
import time
def index(x,y):
    start=time.time()
    print('index %s %s' %(x,y))
    stop=time.time()
    print(stop - start)
index(11,22)

# python3 time.py
index 11 22
6.0558319091796875e-05

 

脚本二

解决了方案二代码冗余问题,但是带来一个新问题,即函数的调用方式改变了

#!/bin/python3
# -*- coding: utf-8 -*-
import time
def index(x,y):
    time.sleep(3)
    print('index %s %s' %(x,y))

start=time.time()
index(11,22)
stop=time.time()
print(stop - start)

start=time.time()        #代码冗余,重复代码过多
index(66,88)
stop=time.time()
print(stop - start)

# python3  time1.py
index 11 22
3.0020477771759033
index 66 88
3.0020477771759033

脚本三

解决了方案二代码冗余问题,但是带来一个新问题,即函数的调用方式改变了

#!/bin/python3
# -*- coding: utf-8 -*-
import time
def index(x,y):
    time.sleep(3)
    print('index %s %s' %(x,y))
def wrapper ():
    start=time.time()
    index(11,22)
    stop=time.time()
    print(stop - start)
wrapper ()

# python3 time2.py
index 11 22
3.0029959678649902

 脚本四

装饰器实现

# cat 1.py
#!/bin/python3
# -*- coding: utf-8 -*-
import time
def index(x,y,z):
    time.sleep(6)
    print('index %s %s %s' %(x,y,z))

def home(name):                    #新增功能代码
    time.sleep(6)
    print('welcome %s to home' %name)
    return 666

def outter(func):
    def wrapper (*args,**kwargs):
        start=time.time()
        func(*args,**kwargs)
        stop=time.time()
        print(stop - start)
    return wrapper

index=outter(index)
home=outter(home)            #调用新功能。偷梁换柱:home这个名字指向的wrapper函数的内存地址

h=index(22,66,88)
print('返回值:',h)

res=home('wei')            #res=wrapper('wei')
l=home(name='wei')
print('返回值:',res)
print('返回值:',l)

# python3 1.py
index 22 66 88
6.006062984466553
返回值: None
welcome wei to home
6.005392074584961
welcome wei to home
6.001788139343262
返回值: None
返回值: None

脚本五

将wrapper做的像wrapper一样

# cat 1.py
#!/bin/python3
# -*- coding: utf-8 -*-
import time
def index(x,y,z):
    time.sleep(6)
    print('index %s %s %s' %(x,y,z))

def home(name):                    #新增功能代码
    time.sleep(6)
    print('welcome %s to home' %name)
    return 666

def outter(func):
    def wrapper (*args,**kwargs):
        start=time.time()
        res=func(*args,**kwargs)
        stop=time.time()
        print(stop - start)
        return res
    return wrapper

index=outter(index)
home=outter(home)

h=index(22,66,88)
print('返回值:',h)

res=home('wei')
l=home(name='wei')
print('返回值:',res)
print('返回值:',l)

# python3 1.py
index 22 66 88
6.004750490188599
返回值: None
welcome wei to home
6.0060179233551025
welcome wei to home
6.005538702011108
返回值: 666
返回值: 666

脚本六

使用语法糖@优化

#!/bin/python3
# -*- coding: utf-8 -*-
import time

def  timmer(func):          #装饰器
    def wrapper (*args,**kwargs):
        start=time.time()
        res=func(*args,**kwargs)
        stop=time.time()
        print(stop - start)
        return res
    return wrapper

@timmer         #在被装饰对象的正上方的单独一行,写@装饰器名字,相当于index=outter(index)
def index(x,y,z):
    time.sleep(6)
    print('index %s %s %s' %(x,y,z))

@timmer         #相当于home=outter(home)
def home(name):                    #新增功能代码
    time.sleep(6)
    print('welcome %s to home' %name)
    return 666


index(x=1,y=2,z=3)
home('wei')

# python3 2.py
index 1 2 3
6.005947828292847
welcome wei to home
6.0057384967803955

 

posted @ 2022-05-14 10:40  九尾cat  阅读(229)  评论(0编辑  收藏  举报