Python2/3 list set性能测试

前言

程序员最悲伤的故事莫过于写了一段代码,编译没错,运行出错
写机器学习最悲伤的故事莫过于饰演了一个算法,运行的时候却卡的跑不起来

这些故事我都经历过,直到刚刚我遇到了一个更悲伤的的套路:

写了段程序,为了性能,查了网上的博客,写完了自己测试却发现前辈们博客上的结果是错的.是错的,是错的
请允许我做个悲伤的表情

而事情的真实面貌是这样的.刚刚我查了python中list与set性能的测试,几个博客写的都是set性能几十倍甚至几百倍于list?,于是我用set写完了我的程序,写完后有点无聊决定自己测试一下set和list的性能到底差多少.然后….然后就没有然后了.

愚蠢的人类啊

测试结果

# Python2.7.13

The 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-05

The 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-05

The 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

多次测试结果与上文显示结果类似,从最后测试效果看:
与前辈们的测试恰恰相反.
无论是类型转变还是集合操作现在版本的list(list转set)都具有比较明显的优势,甚至一些集合操作的性能list平均要比set快上个几十几百倍=- =

还有这种操作?

不说了,我还是去把我刚写的程序中的set类型都换回list吧.

没错这是最新的操作

测试代码下载

百度云链接http://pan.baidu.com/s/1nvKA4Al

原文地址:https://www.cnblogs.com/fonttian/p/9162816.html