1.3 python迭代工具最小计时描述
timertool.py:
timer_compatible():根据win版、unix版、python版本选择对应计时函数计算总时间。
mintime():计算每个迭代工具执行的最小时间。
timeiterevn.py:
timeiter_compatible():计算每个迭代工具执行的总时间、平均时间、最小时间。
示例
#timertool.pyimport time,sysreps = 1000repslist = range(reps)if sys.version[:3]<'3.3': if sys.platform[:3] == 'win': timefunc = time.clock else: timefunc = time.timeelse: timefunc = time.perf_counter def trace(*args): #print('args={}'.format(args)) passdef 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 [%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系统版本对应方法
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)1.5 sys平台版本描述
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')1.6 sorted按键排序用法
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
创基Type-C连接器定制生产厂家让你得到不仅是喜欢
灰尘去无踪,家用手持吸尘器助力健康生活
机器人再次对战人类最强大脑 这次换成人脸识别
GE推出新款金属3D打印机、材料以及应用软件
测试测量企业7月齐聚成都,共同探讨行业发展方向
python迭代调用内置函数计时比较(下)
最新版电信资费营销行为规范详细解读
什么蓝牙耳机品质比较好?盘点四款音质好的蓝牙耳机
造纸行业数字化转型有什么痛点?如何实现?
关于宝骏RS-5斑马系统的应用的解读
动差放大器的来源
阻容降压电路的原理及应用电路
如何区分电源是隔离与非隔离?
Nokia 曝光新旗舰将搭载骁龙835 配备虹膜识别功能
UFSA扩大UFS生态系统,增加可移除式手机存储卡和相关技术的供应商
多尼卡再次赢得中东某航空公司客舱Wi-Fi扩容项目
默克Glenn Young:OLED市场韩国仍极具战略意义
太赫兹技术及其应用详解
谷歌发布AndroidQ的第二个测试版 继续对折叠屏适应
如何避免实施预测性维护时面临的三个常见障碍