Python內(nèi)置數(shù)據(jù)類型list各方法的性能測試過程解析
這篇文章主要介紹了Python內(nèi)置數(shù)據(jù)類型list各方法的性能測試過程解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
測試環(huán)境
本文所涉及的代碼均在MacOS系統(tǒng)與CentOS7下測試,使用的Python版本為3.6.8。
測試模塊
測試用的模塊是Python內(nèi)置的timeit模塊:
timeit模塊可以用來測試一小段Python代碼的執(zhí)行速度。
Timer類
class timeit.Timer(stmt='pass', setup='pass', timer=<timer function>)
Timer是測量小段代碼執(zhí)行速度的類。
stmt參數(shù)是要測試的代碼語句(statment);
setup參數(shù)是運行代碼時需要的設(shè)置;
timer參數(shù)是一個定時器函數(shù),與平臺有關(guān)。
Timer類的timeit方法
timeit.Timer.timeit(number=1000000)
Timer類中測試語句執(zhí)行速度的對象方法。number參數(shù)是測試代碼時的測試次數(shù),默認為1000000次。方法返回執(zhí)行代碼的平均耗時,一個float類型的秒數(shù)。
列表內(nèi)置方法的性能測試
我們知道,生成一個列表可以使用列表生成式或者append、insert、extend這些方法,現(xiàn)在我們來看一下這些方法的執(zhí)行效率:
from timeit import Timer
def test_list():
lst = list(range(1000))
def test_generation():
lst = [i for i in range(1000)]
def test_append():
lst = []
for i in range(1000):
lst.append(i)
def test_add():
lst = []
for i in range(1000):
lst += [i]
# 在列表的頭部insert
def test_insert_zero():
lst = []
for i in range(1000):
lst.insert(0,i)
# 在列表的尾部insert
def test_insert_end():
lst = []
for i in range(1000):
lst.insert(-1,i)
def test_extend():
lst = []
lst.extend(list(range(1000)))
t1 = Timer("test_list()","from __main__ import test_list")
print(f"test_list takes {t1.timeit(number=1000)} seconds")
t2 = Timer("test_generation()","from __main__ import test_generation")
print(f"test_generation takes {t2.timeit(number=1000)} seconds")
t3 = Timer("test_append()","from __main__ import test_append")
print(f"test_append takes {t3.timeit(number=1000)} seconds")
t4 = Timer("test_add()","from __main__ import test_add")
print(f"test_add takes {t4.timeit(number=1000)} seconds")
t5 = Timer("test_insert_zero()","from __main__ import test_insert_zero")
print(f"test_insert_zero takes {t5.timeit(number=1000)} seconds")
t6 = Timer("test_insert_end()","from __main__ import test_insert_end")
print(f"test_insert_end takes {t6.timeit(number=1000)} seconds")
t7 = Timer("test_extend()","from __main__ import test_extend")
print(f"test_extend takes {t7.timeit(number=1000)} seconds")
我們先看看在MacOS系統(tǒng)下,執(zhí)行上面這段代碼的結(jié)果:
""" test_list takes 0.012904746999993222 seconds test_generation takes 0.03530399600003875 seconds test_append takes 0.0865129750000051 seconds test_add takes 0.08066114099983679 seconds test_insert_zero takes 0.30594958500023495 seconds test_insert_end takes 0.1522782449992519 seconds test_extend takes 0.017534753999825625 seconds """
我們可以看到:直接使用list方法強轉(zhuǎn)的效率最高,其次是使用列表生成式,而append與直接加的方式緊隨其后并且二者的效率相當(dāng);insert方法的執(zhí)行效率最低——并且從頭插入的效率要低于從尾部插入的效率!最后我們將強轉(zhuǎn)的列表使用extend方法放入到新的列表中的過程效率并沒有減少多少。
然后試試在Linux系統(tǒng)下的執(zhí)行結(jié)果:

列表pop方法的性能測試
pop可以從第0各位置刪除元素,也可以從最后位置刪除元素(默認刪除最后面的元素),現(xiàn)在我們來測試一下兩種從不同位置刪除元素的性能對比:
from timeit import Timer
def test_pop_zero():
lst = list(range(2000))
for i in range(2000):
lst.pop(0)
def test_pop_end():
lst = list(range(2000))
for i in range(2000):
lst.pop()
t1 = Timer("test_pop_zero()","from __main__ import test_pop_zero")
print(f"test_pop_zero takes {t1.timeit(number=1000)} seconds")
t2 = Timer("test_pop_end()","from __main__ import test_pop_end")
print(f"test_pop_end takes {t2.timeit(number=1000)} seconds")
在MacOS下程序的執(zhí)行結(jié)果為:
test_pop_zero takes 0.5015365449999081 seconds test_pop_end takes 0.22170215499954793 seconds
然后我們來試試Linux系統(tǒng)中的執(zhí)行結(jié)果:

可以看到:從列表的尾部刪除元素的效率要比從頭部刪除的效率高很多!
關(guān)于列表insert方法的一個小坑
如果想使用insert方法生成一個列表[0,1,2,3,4,5]的話(當(dāng)然使用insert方法效率會低很多,建議使用其他的方法)會有一個這樣的問題,在此記錄一下:
def test_insert():
lst = []
for i in range(6):
lst.insert(-1,i)
print(lst)
test_insert()
結(jié)果竟然是這樣的——第一個元素竟然一直在最后!
[0] [1, 0] [1, 2, 0] [1, 2, 3, 0] [1, 2, 3, 4, 0] [1, 2, 3, 4, 5, 0]
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Python中內(nèi)置數(shù)據(jù)類型list,tuple,dict,set的區(qū)別和用法
- Python內(nèi)置數(shù)據(jù)類型詳解
- python基礎(chǔ)教程之基本內(nèi)置數(shù)據(jù)類型介紹
- Python語言內(nèi)置數(shù)據(jù)類型
- python內(nèi)置數(shù)據(jù)類型之列表操作
- python入門課程第四講之內(nèi)置數(shù)據(jù)類型有哪些
- Python的內(nèi)置數(shù)據(jù)類型中的數(shù)字
- Python內(nèi)置數(shù)據(jù)類型中的集合詳解
- python內(nèi)置數(shù)據(jù)類型使用方法和繼承關(guān)系
- 探索Python內(nèi)置數(shù)據(jù)類型的精髓與應(yīng)用
相關(guān)文章
Pycharm 如何設(shè)置HTML文件自動補全代碼或標(biāo)簽
這篇文章主要介紹了Pycharm 如何設(shè)置HTML文件自動補全代碼或標(biāo)簽,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-05-05
如何利用python多線程爬取天氣網(wǎng)站圖片并保存
最近做個天 氣方面的APP需要用到一些天氣數(shù)據(jù),所以下面這篇文章主要給大家介紹了關(guān)于如何利用python多線程爬取天氣網(wǎng)站圖片并保存的相關(guān)資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下2021-11-11
python按照行來讀取txt文件全部內(nèi)容(去除空行處理掉\t,\n后以列表方式返回)
這篇文章主要介紹了python按照行來讀取txt文件全部內(nèi)容 ,去除空行,處理掉\t,\n后,以列表方式返回,本文通過實例代碼給大家介紹的非常詳細,需要的朋友可以參考下2023-06-06
詳解python如何提取瀏覽器中保存的網(wǎng)站登錄用戶名密碼
很多瀏覽器都貼心地提供了保存用戶密碼功能,用戶一旦開啟,就不需要每次都輸入用戶名、密碼,非常方便,作為python腳本,能否拿到用戶提前保存在瀏覽器中的用戶名密碼,用以自動登錄呢,下面我們就來看看吧2023-08-08

