python迭代工具自動調用迭代對象next方法,對迭代對象進行遍歷。
python的for循環、列表解析、map方法、生成器表達式、生成器方法都是迭代工具。
python可迭代對象包括:字符串、列表、元組、字典、集合、range、enumerate、文件等。
1.1 python迭代調用內置函數計時比較
描述
timetool.py:計時模塊,循環調用func函數1000次,返回使用時間和最后一次調用結果。
time.perf_counter():返回性能計數器的值,單位為秒。兩次調用之間的差值用于計時。
timeiterenv.py:各種迭代環境調用內置函數ord(),返回列表。對各種迭代函數調用計時模塊的計時函數進行計時,將計時結果存放在列表,并且按從低到高的順序對計時結果進行排序。
sorted():key = lambda x:x[1],按自定義鍵函數進行排序,x為sorted的第1個參數對應排序對象,x[1]表示按第1個索引值進行排序,本例中對應函數計時結果。
通過例子得出:迭代環境調用內置函數耗時從低到高的順序為:
| NO | 函數 | 描述 |
|---|---|---|
| 1 | mapCall | map迭代工具 |
| 2 | listComp | 列表解析 |
| 3 | genFunc | 生成器函數 |
| 4 | genExpr | 生成器表達式 |
| 5 | forloop | for循環 |
示例
# timetool.py
import time
reps = 1000
repslist = range(reps)
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)
# timeiterenv.py
import sys,timertool
s = '梯閱線條tyxt'*1000
def forloop():
res = []
for x in s:
res.append(ord(x))
return res
def listComp():
return [ord(x) for x in s]
def mapCall():
return list(map(ord,s))
def genExpr():
return list(ord(x) for x in s)
def genFunc():
def gen():
for x in s:
yield ord(x)
return list(gen())
print(sys.version)
reslist=[]
for test in (forloop,listComp,mapCall,genExpr,genFunc):
usetime,result = timertool.timer(test)
reslist.append((test.__name__,usetime,result[0],result[-1],len(result)))
print('-'*33)
reslistsort=sorted(reslist,key = lambda x:x[1])
for L in reslistsort:
print('%-9s:%.5f=>[%s....%s....%s]'%(L[0],L[1],L[2],L[3],L[4]))
print('-'*33)
# 迭代調用內置函數計時比較結果
# 3.7.8 (tags/v3.7.8:4b47a5b6ba, Jun 28 2020, 07:55:33) [MSC v.1916 32 bit (Intel)]
# ---------------------------------
# mapCall :0.20925=>[26799....116....8000]
# listComp :0.42197=>[26799....116....8000]
# genFunc :0.57103=>[26799....116....8000]
# genExpr :0.57259=>[26799....116....8000]
# forloop :0.66177=>[26799....116....8000]
# ---------------------------------
1.2 python迭代調用用戶函數計時比較
描述
python各種迭代環境調用用戶函數ord(x)+1,進行計時比較。
timertool.py不變,修改timeiterevn.py即可。
通過例子得出:迭代環境調用用戶函數耗時從低到高的順序為:
| NO | 函數 | 描述 |
|---|---|---|
| 1 | listComp | 列表解析 |
| 2 | genExpr | 生成器表達式 |
| 3 | genFunc | 生成器函數 |
| 4 | forloop | for循環 |
| 5 | mapCall | map迭代工具 |
示例
# 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())
commstr = '# '
print(commstr+str(sys.version))
reslist=[]
for test in (forloop,listComp,mapCall,genExpr,genFunc):
usetime,result = timertool.timer(test)
reslist.append((test.__name__,usetime,result[0],result[-1],len(result)))
print(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]))
print(commstr+'-'*33)
# 調用用戶函數計時比較結果
# 3.7.8 (tags/v3.7.8:4b47a5b6ba, Jun 28 2020, 07:55:33) [MSC v.1916 32 bit (Intel)]
# ---------------------------------
# listComp :0.50272=>[26799....116....8000]
# genExpr :0.83316=>[26800....117....8000]
# genFunc :0.85477=>[26800....117....8000]
# forloop :0.94426=>[26800....117....8000]
# mapCall :0.96591=>[26800....117....8000]
# ---------------------------------
聲明:本文內容及配圖由入駐作者撰寫或者入駐合作網站授權轉載。文章觀點僅代表作者本人,不代表電子發燒友網立場。文章及其配圖僅供工程師學習之用,如有內容侵權或者其他違規問題,請聯系本站處理。
舉報投訴
-
生成器
+關注
關注
7文章
322瀏覽量
22706 -
python
+關注
關注
57文章
4876瀏覽量
90022 -
for循環
+關注
關注
0文章
61瀏覽量
2884
發布評論請先 登錄
相關推薦
熱點推薦
一文詳解python調用函數
函數被定義后,本身是不會自動執行的,只有在被調用后,函數才會被執行,得到相應的結果。但是在 Python 中我們要注意一個關鍵點,就是Python
發表于 10-01 10:45
?1401次閱讀
快速掌握Python的遞歸函數與匿名函數調用
也有迭代的需求,即將自定義類型定義成迭代器類型即可(需要在類里實現__iter__()和__next__()方法,可供next和iter函數調用該對象)。for循環本質
發表于 07-19 16:22
python的常用函數有哪些
map() 是 Python 內置的高階函數,它接收一個函數 f 和一個list ,并通過把函數 f 依次作用在list 的每個元素
發表于 02-25 11:52
?9次下載
python提供的68個內置函數詳解
? 內置函數就是Python給你提供的,拿來直接用的函數,比如print.,input等。 截止到python版本3.6.2 ,
進階必備的68個Python 內置函數分析
來源: pypypypy 內置函數就是Python給你提供的,拿來直接用的函數,比如print.,input等。 截止到python版本3.
python迭代器詳解
] for i in alist:... print(i)...012345 2. 是否可迭代? 對 Python 比較熟悉的朋友,肯定知道哪些數據類型是可迭代的,哪些是不可
Python支持遞歸函數
Python支持遞歸函數——即直接或間接地調用自身以進行循環的函數。遞歸是頗為高級的話題,并且它在Python中相對少見。然而,它是一項應該
python常用的內置函數和模塊
python數字包含常用的內置函數和模塊,比如pow()、abs()、floor()、int()等函數,以及math、random等模塊。
python函數與函數之間的調用
函數與函數之間的調用 3.1 第一種情況 程序代碼如下: def x ( f ): def y (): print ( 1 ) return y def f (): print ( 2 )x(f
python調用math函數的方法
在Python編程中,數學函數是非常重要的工具,我們可以使用它們進行各種數值計算、幾何運算和統計分析等操作。Python的標準庫中內置了很多數學函數
不屬于python的內置函數
Python是一種高級編程語言,它提供了許多內置函數,可以幫助開發人員更輕松地處理各種任務。但是,在Python中并非所有的函數都是
python迭代調用內置函數計時比較(上)
評論