电子说
描述
timertool.py:
timer_compatible():根据win版、Unix版、python版本选择对应计时函数计算总时间。
mintime():计算每个迭代工具执行的最小时间。
timeiterevn.py:
timeiter_compatible():计算每个迭代工具执行的总时间、平均时间、最小时间。
示例
#timertool.py
import time,sys
reps = 1000
repslist = range(reps)
if sys.version[:3]<'3.3':
if sys.platform[:3] == 'win':
timefunc = time.clock
else:
timefunc = time.time
else:
timefunc = time.perf_counter
def trace(*args):
#print('args={}'.format(args))
pass
def timer(func,*pargs,**kargs):
begin = time.perf_counter()
for i in repslist:
ret = func(*pargs,**kargs)
usetime = time.perf_counter() - begin
return (usetime,ret)
def timer_compatible(func,*pargs,**kargs):
_reps = kargs.pop('_reps',1000)
trace(func,pargs,kargs,_reps)
repslist = range(_reps)
begin = timefunc()
for i in repslist:
ret = func(*pargs,**kargs)
usetime = timefunc() - begin
return (usetime,ret)
def mintime(func,*pargs,**kargs):
_reps = kargs.pop('_reps',50)
mintime = 2 ** 32
for i in range(_reps):
(usetime,ret) = timer_compatible(func,*pargs,_reps=1,**kargs)
if usetime < mintime:
mintime = usetime
return (mintime,ret)
# timeiterevn.py
import sys,timertool
s = '梯阅线条tyxt'*1000
def forloop():
res = []
for x in s:
res.append(ord(x)+1)
return res
def listComp():
return [ord(x) for x in s]
def mapCall():
return list(map(lambda x:ord(x)+1,s))
def genExpr():
return list(ord(x)+1 for x in s)
def genFunc():
def gen():
for x in s:
yield ord(x)+1
return list(gen())
def commstr(s):
commstr = '# '+s
print(commstr)
functp = (forloop,listComp,mapCall,genExpr,genFunc)
timetp = (timertool.timer_compatible,timertool.mintime)
commstr('-'*33)
commstr(str(sys.version))
def timeiter():
reslist=[]
for test in funcList:
usetime,result = timertool.timer(test)
reslist.append((test.__name__,usetime,result[0],result[-1],len(result)))
commstr('-'*33)
reslistsort=sorted(reslist,key = lambda x:x[1])
for L in reslistsort:
#print(commstr+'%-9s:%.5f=>[%s....%s....%s]'%(L[0],L[1],L[2],L[3],L[4]))
commstr('%-9s:%.5f=>[%s....%s....%s]'%(L[0],L[1],L[2],L[3],L[4]))
commstr('-'*33)
def timeiter_compatible():
_reps = 1000
commstr('-'*33)
for ttp in timetp:
reslist=[]
commstr('<{}>'.format(ttp.__name__))
for ftp in functp:
usetime,result = ttp(ftp,_reps=_reps)
reslist.append((ftp.__name__,usetime,result[0],result[-1],len(result)))
commstr('-'*33)
reslistsort=sorted(reslist,key = lambda x:x[1])
if ttp.__name__ == 'timer_compatible':
commstr('总时间排序')
else:
commstr('最小时间排序')
commstr('-'*33)
for L in reslistsort:
commstr('%-9s:%.5f=>[%s....%s....%s]'%(L[0],L[1],L[2],L[3],L[4]))
commstr('-'*33)
if ttp.__name__ == 'timer_compatible':
commstr('平均时间排序')
commstr('-'*33)
for L in reslistsort:
commstr('%-9s:%.5f=>[%s....%s....%s]'%(L[0],(L[1]/_reps),L[2],L[3],L[4]))
commstr('-'*33)
timeiter_compatible()
# ---------------------------------
# 3.7.8 (tags/v3.7.8:4b47a5b6ba, Jun 28 2020, 07:55:33) [MSC v.1916 32 bit (Intel)]
# ---------------------------------
#
# ---------------------------------
# 总时间排序
# ---------------------------------
# listComp :0.52310=>[26799....116....8000]
# genFunc :0.92414=>[26800....117....8000]
# genExpr :0.94791=>[26800....117....8000]
# forloop :1.01522=>[26800....117....8000]
# mapCall :1.12953=>[26800....117....8000]
# ---------------------------------
# 平均时间排序
# ---------------------------------
# listComp :0.00052=>[26799....116....8000]
# genFunc :0.00092=>[26800....117....8000]
# genExpr :0.00095=>[26800....117....8000]
# forloop :0.00102=>[26800....117....8000]
# mapCall :0.00113=>[26800....117....8000]
# ---------------------------------
#
# ---------------------------------
# 最小时间排序
# ---------------------------------
# listComp :0.00039=>[26799....116....8000]
# genFunc :0.00065=>[26800....117....8000]
# genExpr :0.00066=>[26800....117....8000]
# forloop :0.00072=>[26800....117....8000]
# mapCall :0.00073=>[26800....117....8000]
# ---------------------------------
描述
python的time模块对Windows、Unix、python版本提供不同计时方法。
NO | 系统版本 | 对应方法 |
---|---|---|
1 | Windows | time.clock() |
2 | Unix | time.time() |
3 | =python3.8 | time.perf_counter(),python3.3开始支持 |
两次调用之间的时间差用于计时。
示例
>>> import time
>>> begin = time.clock()
>>> end = time.clock()
>>> use=end - begin
>>> begin,end,use
(782.9449924, 793.3414938, 10.39650139999992)
>>> begin = time.time()
>>> end = time.time()
>>> use = end - begin
>>> begin,end,use
(1674368678.73148, 1674368685.6409733, 6.909493446350098)
>>> begin = time.perf_counter()
>>> end = time.perf_counter()
>>> use = end - begin
>>> begin,end,use
(899.3102242, 908.7626699, 9.452445699999998)
描述
python通过sys模块获取平台版本信息。
NO | 属性 | 描述 |
---|---|---|
1 | sys.version | python版本 |
2 | sys.platform | 系统平台 |
示例
>>> import sys
>>> v=sys.version
>>> pf=sys.platform
>>> v
'3.7.8 (tags/v3.7.8:4b47a5b6ba, Jun 28 2020, 07:55:33) [MSC v.1916 32 bit (Intel)]'
>>> pf
'win32'
>>> v[:3],pf[:3]
('3.7', 'win')
用法
sorted(iterable, /, *, key=None, reverse=False)
描述
python内置函数sorted(可迭代对象,key)属于迭代工具,按指定key对可迭代对象进行排序。
key:自定义排序函数。
reverse:默认False为升序。
示例
>>> zs={'name':'张三','yuwen':90,'shuxue':100,'english':60}
>>> ls={'name':'李四','yuwen':91,'shuxue':95,'english':85}
>>> ww={'name':'王五','yuwen':80,'shuxue':99,'english':82}
>>> classOne=[zs,ls,ww]
>>> for i,stu in zip(range(1,len(classOneSort)+1),classOneSort):
zf = stu['yuwen']+stu['shuxue']+stu['english']
print("总分第{i:0>3}名:{d[name]:>4},语文={d[yuwen]:>3},数学={d[shuxue]:>3},英语={d[english]:>3},总分={zf:>3}".format(d=stu,zf=zf,i=i))
总分第001名: 李四,语文= 91,数学= 95,英语= 85,总分=271
总分第002名: 王五,语文= 80,数学= 99,英语= 82,总分=261
总分第003名: 张三,语文= 90,数学=100,英语= 60,总分=250
全部0条评论
快来发表一下你的评论吧 !