Python2/3 list set性能测试
前言
程序員最悲傷的故事莫過于寫了一段代碼,編譯沒錯(cuò),運(yùn)行出錯(cuò)
寫機(jī)器學(xué)習(xí)最悲傷的故事莫過于飾演了一個(gè)算法,運(yùn)行的時(shí)候卻卡的跑不起來
這些故事我都經(jīng)歷過,直到剛剛我遇到了一個(gè)更悲傷的的套路:
寫了段程序,為了性能,查了網(wǎng)上的博客,寫完了自己測試卻發(fā)現(xiàn)前輩們博客上的結(jié)果是錯(cuò)的.是錯(cuò)的,是錯(cuò)的
而事情的真實(shí)面貌是這樣的.剛剛我查了python中l(wèi)ist與set性能的測試,幾個(gè)博客寫的都是set性能幾十倍甚至幾百倍于list?,于是我用set寫完了我的程序,寫完后有點(diǎn)無聊決定自己測試一下set和list的性能到底差多少.然后….然后就沒有然后了.
測試結(jié)果
# Python2.7.13The time of list to set : 0.000111617786487 The time of list to set2 -set3 : 7.74054381383e-05 The time of list to set2 & set3 : 3.67782744746e-05 The time of list to set2 | set3 : 0.000196293348649 The time of list to set2 != set3 : 1.71061741742e-06 The time of list to set2 == set3 : 4.27654354355e-07 The time of list to set2 in set3 : 9.23733405407e-05 The time of list to set1007 = set2 not in set3 : 6.92800054056e-05The time of set to list : 2.73698786787e-05 The time of list to list2 & list3 : 8.55308708711e-07 The time of list to list2 | list3 : 4.27654354355e-07 The time of list to list2 != list3 : 4.27654354355e-06 The time of list to list2 == list3 : 4.27654354355e-07 The time of list to list2 in list3 : 8.4247907808e-05 The time of list to list1007 = list2 not in list3 : 6.11545726728e-05 # Python3.6.1 The time of list to set : 9.451161231251095e-05 The time of list to set2 -set3 : 7.312889459474832e-05 The time of list to set2 & set3 : 5.260148558569621e-05 The time of list to set2 | set3 : 0.00010220939069090543 The time of list to set2 != set3 : 8.553087087106309e-07 The time of list to set2 == set3 : 4.276543543550986e-07 The time of list to set2 in set3 : 5.473975735747257e-05 The time of list to set1007 = set2 not in set3 : 2.352098948953888e-05The time of set to list : 1.0263704504526053e-05 The time of list to list2 & list3 : 4.276543543550986e-07 The time of list to list2 | list3 : 1.2829630630657295e-06 The time of list to list2 != list3 : 8.553087087106309e-07 The time of list to list2 == list3 : 0.0 The time of list to list2 in list3 : 2.4376298198249295e-05 The time of list to list1007 = list2 not in list3 : 2.3520989489539098e-05多次測試結(jié)果與上文顯示結(jié)果類似,從最后測試效果看:
與前輩們的測試恰恰相反.
無論是類型轉(zhuǎn)變還是集合操作現(xiàn)在版本的list(list轉(zhuǎn)set)都具有比較明顯的優(yōu)勢(shì),甚至一些集合操作的性能list平均要比set快上個(gè)幾十幾百倍=- =
不說了,我還是去把我剛寫的程序中的set類型都換回list吧.
測試代碼下載
百度云鏈接http://pan.baidu.com/s/1nvKA4Al
總結(jié)
以上是生活随笔為你收集整理的Python2/3 list set性能测试的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 汉语自然语言处理工具包下载
- 下一篇: Pandas matplotlib 无法