0
  • 聊天消息
  • 系統(tǒng)消息
  • 評論與回復(fù)
登錄后你可以
  • 下載海量資料
  • 學(xué)習(xí)在線課程
  • 觀看技術(shù)視頻
  • 寫文章/發(fā)帖/加入社區(qū)
會員中心
創(chuàng)作中心

完善資料讓更多小伙伴認(rèn)識你,還能領(lǐng)取20積分哦,立即完善>

3天內(nèi)不再提示

python迭代調(diào)用內(nèi)置函數(shù)計時比較(下)

jf_78858299 ? 來源:梯閱線條小梯子 ? 作者:小梯子 ? 2023-02-21 14:56 ? 次閱讀

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
聲明:本文內(nèi)容及配圖由入駐作者撰寫或者入駐合作網(wǎng)站授權(quán)轉(zhuǎn)載。文章觀點(diǎn)僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場。文章及其配圖僅供工程師學(xué)習(xí)之用,如有內(nèi)容侵權(quán)或者其他違規(guī)問題,請聯(lián)系本站處理。 舉報投訴
  • 生成器
    +關(guān)注

    關(guān)注

    7

    文章

    313

    瀏覽量

    20923
  • python
    +關(guān)注

    關(guān)注

    54

    文章

    4759

    瀏覽量

    84294
  • for循環(huán)
    +關(guān)注

    關(guān)注

    0

    文章

    61

    瀏覽量

    2483
收藏 人收藏

    評論

    相關(guān)推薦

    關(guān)于Python巧妙而強(qiáng)大的內(nèi)置函數(shù)

    python內(nèi)置了一些非常巧妙而且強(qiáng)大的內(nèi)置函數(shù),對初學(xué)者來說,一般不怎么用到,我也是用了一段時間python之后才發(fā)現(xiàn),哇還有這么好的
    發(fā)表于 12-14 14:52 ?516次閱讀
    關(guān)于<b class='flag-5'>Python</b>巧妙而強(qiáng)大的<b class='flag-5'>內(nèi)置</b><b class='flag-5'>函數(shù)</b>

    一文詳解python調(diào)用函數(shù)

    函數(shù)被定義后,本身是不會自動執(zhí)行的,只有在被調(diào)用后,函數(shù)才會被執(zhí)行,得到相應(yīng)的結(jié)果。但是在 Python 中我們要注意一個關(guān)鍵點(diǎn),就是Python
    發(fā)表于 10-01 10:45 ?562次閱讀

    快速掌握Python的遞歸函數(shù)與匿名函數(shù)調(diào)用

    函數(shù)Python技術(shù)學(xué)習(xí)中重要的一個環(huán)節(jié),深入掌握該階段的知識內(nèi)容,對于Python技術(shù)能力的提升非常有幫助,這里就針對遞歸函數(shù)與匿名函數(shù)
    發(fā)表于 07-19 16:22

    python迭代

    的,哪些是不可迭代的。但是對新手來說,可能需要借助一些函數(shù)來判別,比如 Python 內(nèi)置的 collections.abc 模塊,這個模塊只有在
    發(fā)表于 02-24 15:42

    python內(nèi)置函數(shù)詳細(xì)資料概述

    本文檔的主要內(nèi)容詳細(xì)介紹的是python內(nèi)置函數(shù)詳細(xì)資料概述。
    發(fā)表于 11-18 08:00 ?0次下載

    python提供的68個內(nèi)置函數(shù)詳解

    ? 內(nèi)置函數(shù)就是Python給你提供的,拿來直接用的函數(shù),比如print.,input等。 截止到python版本3.6.2 ,
    的頭像 發(fā)表于 10-27 09:14 ?2142次閱讀

    進(jìn)階必備的68個Python 內(nèi)置函數(shù)分析

    來源: pypypypy 內(nèi)置函數(shù)就是Python給你提供的,拿來直接用的函數(shù),比如print.,input等。 截止到python版本3.
    的頭像 發(fā)表于 11-01 10:18 ?3953次閱讀

    python迭代器詳解

    ] for i in alist:... print(i)...012345 2. 是否可迭代? 對 Python 比較熟悉的朋友,肯定知道哪些數(shù)據(jù)類型是可迭代的,哪些是不可
    的頭像 發(fā)表于 02-24 15:42 ?1300次閱讀

    Python支持遞歸函數(shù)

    Python支持遞歸函數(shù)——即直接或間接地調(diào)用自身以進(jìn)行循環(huán)的函數(shù)。遞歸是頗為高級的話題,并且它在Python中相對少見。然而,它是一項應(yīng)該
    的頭像 發(fā)表于 02-21 14:28 ?602次閱讀

    python迭代調(diào)用內(nèi)置函數(shù)計時比較(上)

    python迭代工具自動調(diào)用迭代對象next方法,對迭代對象進(jìn)行遍歷。 python的fo
    的頭像 發(fā)表于 02-21 14:56 ?573次閱讀

    python常用的內(nèi)置函數(shù)和模塊

    python數(shù)字包含常用的內(nèi)置函數(shù)和模塊,比如pow()、abs()、floor()、int()等函數(shù),以及math、random等模塊。
    的頭像 發(fā)表于 03-10 10:10 ?880次閱讀

    python定義函數(shù)調(diào)用函數(shù)的順序

    定義函數(shù)調(diào)用函數(shù)的順序 函數(shù)被定義后,本身是不會自動執(zhí)行的,只有在被調(diào)用后,函數(shù)才會被執(zhí)行,得
    的頭像 發(fā)表于 10-04 17:17 ?1169次閱讀

    python函數(shù)函數(shù)之間的調(diào)用

    函數(shù)函數(shù)之間的調(diào)用 3.1 第一種情況 程序代碼如下: def x ( f ): def y (): print ( 1 ) return y def f (): print ( 2 )x(f
    的頭像 發(fā)表于 10-04 17:17 ?530次閱讀

    python調(diào)用math函數(shù)的方法

    Python編程中,數(shù)學(xué)函數(shù)是非常重要的工具,我們可以使用它們進(jìn)行各種數(shù)值計算、幾何運(yùn)算和統(tǒng)計分析等操作。Python的標(biāo)準(zhǔn)庫中內(nèi)置了很多數(shù)學(xué)函數(shù)
    的頭像 發(fā)表于 11-22 11:01 ?2458次閱讀

    不屬于python內(nèi)置函數(shù)

    Python是一種高級編程語言,它提供了許多內(nèi)置函數(shù),可以幫助開發(fā)人員更輕松地處理各種任務(wù)。但是,在Python中并非所有的函數(shù)都是
    的頭像 發(fā)表于 11-29 14:27 ?1265次閱讀