1.3 python迭代工具最小計時
描述
timertool.py:
timer_compatible():根據(jù)win版、Unix版、python版本選擇對應(yīng)計時函數(shù)計算總時間。
mintime():計算每個迭代工具執(zhí)行的最小時間。
timeiterevn.py:
timeiter_compatible():計算每個迭代工具執(zhí)行的總時間、平均時間、最小時間。
示例
#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]
# ---------------------------------
1.4 time計時
描述
python的time模塊對Windows、Unix、python版本提供不同計時方法。
NO | 系統(tǒng)版本 | 對應(yīng)方法 |
---|---|---|
1 | Windows | time.clock() |
2 | Unix | time.time() |
3 | =python3.8 | time.perf_counter(),python3.3開始支持 |
兩次調(diào)用之間的時間差用于計時。
示例
>>> 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)
1.5 sys平臺版本
描述
python通過sys模塊獲取平臺版本信息。
NO | 屬性 | 描述 |
---|---|---|
1 | sys.version | python版本 |
2 | sys.platform | 系統(tǒng)平臺 |
示例
>>> 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')
1.6 sorted按鍵排序
用法
sorted(iterable, /, *, key=None, reverse=False)
描述
python內(nèi)置函數(shù)sorted(可迭代對象,key)屬于迭代工具,按指定key對可迭代對象進(jìn)行排序。
key:自定義排序函數(shù)。
reverse:默認(rèn)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},數(shù)學(xué)={d[shuxue]:>3},英語={d[english]:>3},總分={zf:>3}".format(d=stu,zf=zf,i=i))
總分第001名: 李四,語文= 91,數(shù)學(xué)= 95,英語= 85,總分=271
總分第002名: 王五,語文= 80,數(shù)學(xué)= 99,英語= 82,總分=261
總分第003名: 張三,語文= 90,數(shù)學(xué)=100,英語= 60,總分=250
-
生成器
+關(guān)注
關(guān)注
7文章
315瀏覽量
21003 -
python
+關(guān)注
關(guān)注
56文章
4793瀏覽量
84631 -
for循環(huán)
+關(guān)注
關(guān)注
0文章
61瀏覽量
2502
發(fā)布評論請先 登錄
相關(guān)推薦
評論