python函数装饰器参数统计调用时间和次数

1 python函数装饰器参数统计调用时间和次数

python在函数装饰器外层定义一个函数生成封闭作用域来保存装饰器入参,供装饰器使用。

1.1 装饰器统计调用时间和次数

描述

通过类的可调用实例装饰器来统计函数每次调用时间和总调用时间,以及调用次数。

(1) time.perf_counter()获取当前时间,单位秒;

(2) 调用函数func前和后的时间差,为func的执行时间usetime;

(3) 将每次的执行时间usetime累加,获得总时间alltime;

(4) 每调用一次func,calls计增一次获得总次数;

(5) 比较列表解析和map内置函数生成列表的调用时间;

(6) map在py2返回列表,在py3返回迭代器,需用list转为列表;

示例

>>> import time,sys
>>> class TimeCount:
    def __init__(self,func):
        self.func=func
        self.alltime=0
        self.calls=0
    def __call__(self,*args,**kargs):
        begin=time.perf_counter()
        res=self.func(*args,**kargs)
        usetime=time.perf_counter()-begin
        self.alltime+=usetime
        self.calls+=1
        callstr=' 调用 {0[0]} {0[1]} 次 '
        calltpl=self.func.__name__,self.calls
        timestr='{0[0]}:usetime={0[1]:.6f},alltime={0[2]:.6f}'
        timetpl=self.func.__name__,usetime,self.alltime
        resstr='res[0]={0[0]},res[-1]={0[1]},len(res)={0[2]}'
        restpl=res[0],res[-1],len(res)
        print(callstr.format(calltpl).center(50,'-'))
        print(timestr.format(timetpl))
        print(resstr.format(restpl),'\n')

>>> @TimeCount
def listcomp(N):
    return [x*2 for x in range(N)]

>>> if sys.version_info[0]==2:
    @TimeCount
    def mapcall(N):
        return map((lambda x:x*2),range(N))
else:
    @TimeCount
    def mapcall(N):
        # py3的map返回迭代器需用list转换
        # 否则统计的时间与listcomp不一致
        return list(map((lambda x:x*2),range(N)))

>>> xtpl=5,50000,500000,1000000
>>> tuple(map(listcomp,xtpl))
---------------- 调用 listcomp 1-----------------
listcomp:usetime=0.000005,alltime=0.000005
res[0]=0,res[-1]=8,len(res)=5 

---------------- 调用 listcomp 2-----------------
listcomp:usetime=0.002395,alltime=0.002400
res[0]=0,res[-1]=99998,len(res)=50000 

---------------- 调用 listcomp 3-----------------
listcomp:usetime=0.026660,alltime=0.029060
res[0]=0,res[-1]=999998,len(res)=500000 

---------------- 调用 listcomp 4-----------------
listcomp:usetime=0.053023,alltime=0.082084
res[0]=0,res[-1]=1999998,len(res)=1000000 

(None, None, None, None)
>>> tuple(map(mapcall,xtpl))
----------------- 调用 mapcall 1-----------------
mapcall:usetime=0.000005,alltime=0.000005
res[0]=0,res[-1]=8,len(res)=5 

----------------- 调用 mapcall 2-----------------
mapcall:usetime=0.003963,alltime=0.003968
res[0]=0,res[-1]=99998,len(res)=50000 

----------------- 调用 mapcall 3-----------------
mapcall:usetime=0.041263,alltime=0.045231
res[0]=0,res[-1]=999998,len(res)=500000 

----------------- 调用 mapcall 4-----------------
mapcall:usetime=0.083637,alltime=0.128868
res[0]=0,res[-1]=1999998,len(res)=1000000 

(None, None, None, None)
>>> '{:.6f}'.format(mapcall.alltime)
'0.128868'
>>> '{:.6f}'.format(listcomp.alltime)
'0.082084'
>>> '{:.6f}'.format(mapcall.alltime/listcomp.alltime)
'1.569957'

1.2 嵌套函数统计电影时间和次数

描述

通过嵌套函数来统计函数每次调用时间和总调用时间,以及调用次数。

(1) time.perf_counter()获取当前时间,单位秒;

(2) 调用函数func前和后的时间差,为func的执行时间usetime;

(3) 将每次的执行时间usetime累加,获得总时间alltime;

(4) 每调用一次func,calls计增一次获得总次数;

(5) 比较列表解析和map内置函数生成列表的调用时间;

(6) map在py2返回列表,在py3返回迭代器,需用list转为列表;

(7) 将alltime和calls赋值给func进行返回,供后续获取;

(8) 通过nonlocal修改嵌套作用域的变量alltime和calls;

示例

>>> import time,sys
>>> def timecount_func(func):
    alltime=0
    calls=0
    def tcf_wrapper(*pargs,**kargs):
        begin = time.perf_counter()
        res=func(*pargs,**kargs)
        usetime = time.perf_counter() - begin
        nonlocal alltime
        nonlocal calls
        alltime+=usetime
        calls+=1
        timestr='{0[0]}:usetime={0[1]:.6f},alltime={0[2]:.6f}'
        timetpl=func.__name__,usetime,alltime
        callstr=' 调用 {0[0]} {0[1]} 次 '
        calltpl=func.__name__,calls
        resstr='res[0]={0[0]},res[-1]={0[1]},len(res)={0[2]}'
        restpl=res[0],res[-1],len(res)
        print(callstr.format(calltpl).center(50,'-'))
        print(timestr.format(timetpl))
        print(resstr.format(restpl),'\n')
        func.alltime=alltime
        func.calls=calls
    return tcf_wrapper

>>> def listcomp(N):
    return [x*2 for x in range(N)]

>>> if sys.version_info[0]==2:
    def mapcall(N):
        return map((lambda x:x*2),range(N))
else:
    def mapcall(N):
        return list(map((lambda x:x*2),range(N)))
        
>>> xtpl=5,50000,500000,1000000
>>> tuple(map(timecount_func(listcomp),xtpl))
---------------- 调用 listcomp 1-----------------
listcomp:usetime=0.000005,alltime=0.000005
res[0]=0,res[-1]=8,len(res)=5 

---------------- 调用 listcomp 2-----------------
listcomp:usetime=0.002478,alltime=0.002482
res[0]=0,res[-1]=99998,len(res)=50000 

---------------- 调用 listcomp 3-----------------
listcomp:usetime=0.031966,alltime=0.034448
res[0]=0,res[-1]=999998,len(res)=500000 

---------------- 调用 listcomp 4-----------------
listcomp:usetime=0.068800,alltime=0.103248
res[0]=0,res[-1]=1999998,len(res)=1000000 

(None, None, None, None)
>>> tuple(map(timecount_func(mapcall),xtpl))
----------------- 调用 mapcall 1-----------------
mapcall:usetime=0.000006,alltime=0.000006
res[0]=0,res[-1]=8,len(res)=5 

----------------- 调用 mapcall 2-----------------
mapcall:usetime=0.004435,alltime=0.004441
res[0]=0,res[-1]=99998,len(res)=50000 

----------------- 调用 mapcall 3-----------------
mapcall:usetime=0.041257,alltime=0.045698
res[0]=0,res[-1]=999998,len(res)=500000 

----------------- 调用 mapcall 4-----------------
mapcall:usetime=0.082711,alltime=0.128409
res[0]=0,res[-1]=1999998,len(res)=1000000 

(None, None, None, None)
>>> '{:.6f}'.format(mapcall.alltime)
'0.128409'
>>> '{:.6f}'.format(listcomp.alltime)
'0.103248'
>>> '{:.6f}'.format(mapcall.alltime/listcomp.alltime)
'1.243693'

1.3 函数装饰器参数

通过装饰器参数指定配置选项。在装饰的时候传入一个标签和一个跟踪控制标志。

提供一个输出标签,以及打开或关闭跟踪消息,

用法

def close_scope_func(label='',trace=True):
    def decorator(func):
        def onCall(*args):
            pass
            func(*args)
            pass
    return decorator
    
@close_scope_func('梯阅线条')
def test_func(x):
    pass

test_func('tyxt')

描述

装饰器参数需在外层添加一个函数生成一个封闭作用域用来保存参数,供装饰器使用。

(1) 在装饰器外层定义一个函数,并且指定装饰器需要的入参;

(2) 外层函数需返回一个可调用的装饰器,给到被装饰函数;

(3) 装饰函数时,传入对应入参即可;

示例

>>> import time,sys
>>> def timecount_ctr(label='',trace=True):
    class TimeCount:
        def __init__(self,func):
            self.func=func
            self.alltime=0
            self.calls=0
        def __call__(self,*args,**kargs):
            begin=time.perf_counter()
            res=self.func(*args,**kargs)
            usetime=time.perf_counter()-begin
            self.alltime+=usetime
            self.calls+=1
            if trace:
                callstr=' 调用 {0[0]} {0[1]} 次 '
                callval=self.func.__name__,self.calls
                timestr='{0[0]}:usetime={0[1]:.6f},alltime={0[2]:.6f}'
                timeval=self.func.__name__,usetime,self.alltime
                resstr='res[0]={0[0]},res[-1]={0[1]},len(res)={0[2]}'
                resval=res[0],res[-1],len(res)
                print(label,callstr.format(callval).center(50,'-'))
                print(label,timestr.format(timeval))
                print(label,resstr.format(resval),'\n')
    return TimeCount

>>> @timecount_ctr('[listcomp]==>')
def listcomp(N):
    return [x*2 for x in range(N)]

>>> if sys.version_info[0]==2:
    @timecount_ctr('[mapcall]==>')
    def mapcall(N):
        return map((lambda x:x*2),range(N))
else:
    @timecount_ctr('[mapcall]==>')
    def mapcall(N):
        # py3的map返回迭代器需用list转换
        # 否则统计的时间与listcomp不一致
        return list(map((lambda x:x*2),range(N)))

>>> xtpl=5,50000,500000,1000000
>>> tuple(map(listcomp,xtpl))
[listcomp]==> ---------------- 调用 listcomp 1-----------------
[listcomp]==> listcomp:usetime=0.000005,alltime=0.000005
[listcomp]==> res[0]=0,res[-1]=8,len(res)=5 

[listcomp]==> ---------------- 调用 listcomp 2-----------------
[listcomp]==> listcomp:usetime=0.002641,alltime=0.002646
[listcomp]==> res[0]=0,res[-1]=99998,len(res)=50000 

[listcomp]==> ---------------- 调用 listcomp 3-----------------
[listcomp]==> listcomp:usetime=0.027689,alltime=0.030335
[listcomp]==> res[0]=0,res[-1]=999998,len(res)=500000 

[listcomp]==> ---------------- 调用 listcomp 4-----------------
[listcomp]==> listcomp:usetime=0.052438,alltime=0.082773
[listcomp]==> res[0]=0,res[-1]=1999998,len(res)=1000000 

(None, None, None, None)
>>> tuple(map(mapcall,xtpl))
[mapcall]==> ----------------- 调用 mapcall 1-----------------
[mapcall]==> mapcall:usetime=0.000004,alltime=0.000004
[mapcall]==> res[0]=0,res[-1]=8,len(res)=5 

[mapcall]==> ----------------- 调用 mapcall 2-----------------
[mapcall]==> mapcall:usetime=0.003975,alltime=0.003979
[mapcall]==> res[0]=0,res[-1]=99998,len(res)=50000 

[mapcall]==> ----------------- 调用 mapcall 3-----------------
[mapcall]==> mapcall:usetime=0.040665,alltime=0.044644
[mapcall]==> res[0]=0,res[-1]=999998,len(res)=500000 

[mapcall]==> ----------------- 调用 mapcall 4-----------------
[mapcall]==> mapcall:usetime=0.098298,alltime=0.142942
[mapcall]==> res[0]=0,res[-1]=1999998,len(res)=1000000 

(None, None, None, None)
>>> '{:.6f}'.format(mapcall.alltime)
'0.142942'
>>> '{:.6f}'.format(listcomp.alltime)
'0.082773'
>>> '{:.6f}'.format(mapcall.alltime/listcomp.alltime)
'1.726914'

你可能感兴趣的:(python,python)