赞
踩
测试代码如下:
import timeit,time
from sys import getsizeof as Size
from collections import OrderedDict
import random
List_Test = list(range(5000))
print("The Size of List is : {}".format(Size(List_Test)))
Set_Test = set(range(5000))
print("The Size of Set is : {}".format(Size(Set_Test)))
Tuple_Test = tuple(range(5000))
print("The Size of Tuple is : {}".format(Size(Tuple_Test)))
Dict_Test = {k:k for k in range(5000)}
print("The Size of Dict is : {}".format(Size(Dict_Test)))
OrderedDict_Test = OrderedDict({k:k for k in range(5000)})
print("The Size of OrderedDict is :{}".format(Size(OrderedDict_Test)))
print("\nNow is to test speed\n")
time.sleep(1)
def Create_List():
List = [i for i in range(5000)]
def Test_List():
randomNumber = random.randint(0,5000)
if randomNumber in List_Test:
return False
def Create_Set():
Set = set(i for i in range(5000))
def Test_Set():
randomNumber = random.randint(0,5000)
if randomNumber in Set_Test:
return False
def Create_Tuple():
Tuple = tuple(i for i in range(5000))
def Test_Tuple():
randomNumber = random.randint(0,5000)
if randomNumber in Tuple_Test:
return False
def Create_Dict():
Dict = {k:k for k in range(5000)}
def Test_Dict():
randomNumber = random.randint(0,5000)
if randomNumber in Dict_Test.keys():
return False
def Create_OrderedDict():
orderedDict = OrderedDict({k:k for k in range(5000)})
def Test_OrderedDict():
randomNumber = random.randint(0, 5000)
if randomNumber in OrderedDict_Test.keys():
return False
t = timeit.repeat(stmt="Create_List()",number=50,setup="from __main__ import Create_List", repeat=15)
print("The Time of Create_List : {}".format(sum(t)/len(t)))
t = timeit.repeat(stmt="Create_Tuple()",number=50,setup="from __main__ import Create_Tuple", repeat=15)
print("The Time of Create_Tuple : {}".format(sum(t)/len(t)))
t = timeit.repeat(stmt="Create_Set()",number=50,setup="from __main__ import Create_Set", repeat=15)
print("The Time of Create_Set : {}".format(sum(t)/len(t)))
t = timeit.repeat(stmt="Create_Dict()",number=50,setup="from __main__ import Create_Dict", repeat=15)
print("The Time of Create_Dict : {}".format(sum(t)/len(t)))
t = timeit.repeat(stmt="Create_OrderedDict()",number=50,setup="from __main__ import Create_OrderedDict", repeat=15)
print("The Time of Create_OrderedDict : {}".format(sum(t)/len(t)))
print("\n")
t = timeit.repeat(stmt="Test_List()",number=15,setup="from __main__ import Test_List", repeat=15)
print("The Time of Test_List : {}".format(sum(t)/len(t)))
t = timeit.repeat(stmt="Test_Tuple()",number=15,setup="from __main__ import Test_Tuple", repeat=15)
print("The Time of Test_Tuple : {}".format(sum(t)/len(t)))
t = timeit.repeat(stmt="Test_Set()",number=15,setup="from __main__ import Test_Set", repeat=15)
print("The Time of Test_Set : {}".format(sum(t)/len(t)))
t = timeit.repeat(stmt="Test_Dict()",number=15,setup="from __main__ import Test_Dict", repeat=15)
print("The Time of Test_Dict : {}".format(sum(t)/len(t)))
t = timeit.repeat(stmt="Test_OrderedDict()",number=15,setup="from __main__ import Test_OrderedDict", repeat=15)
print("The Time of Test_OrderedDict : {}".format(sum(t)/len(t)))
print("\nThe end")
结果如下:
The Size of List is : 45112
The Size of Set is : 524512
The Size of Tuple is : 40048
The Size of Dict is : 147560
The Size of OrderedDict is :373160
Now is to test speed
The Time of Create_List : 0.006335740000000042
The Time of Create_Tuple : 0.010433759999999988
The Time of Create_Set : 0.013531839999999977
The Time of Create_Dict : 0.011174999999999964
The Time of Create_OrderedDict : 0.04272052666666668
The Time of Test_List : 0.0003235733333334008
The Time of Test_Tuple : 0.00032904666666671765
The Time of Test_Set : 1.814666666666298e-05
The Time of Test_Dict : 1.924666666669831e-05
The Time of Test_OrderedDict : 2.128666666673477e-05
The end
内存占用:
Size: Tuple < List < Dict < OrderedDict < Set
创建速度比较:
Time of create: List < Tuple < Dict < Set < OrderedDict
查找速度比较(妈也集合和字典查找真几把快):
Time of search: Dict ≈ Set ≈ OrderedDict << Tuple ≈ List # << 大概快了5k多倍
效率最大化结论:
需要索引 + 需要修改 + 需要重复元素 = 列表List
需要索引 + 不需要修改 + 需要重复元素 = 元组Tuple
不需要索引(与顺序无关) + 需要修改 + 不需要重复 = 集合Set
不需要索引(与顺序无关)+ 不需要修改 + 不需要重复 = 冻结集合frozenSet
个人结论:就速度而言,全都使用字典是最好的 (注:在Python3 中,字典自带顺序,但不推荐用)。除非是不需要重复的值,就可以使用Set,其他的都别用了,要么不想修改的可以考虑用下Tuple。要是想省内存空间的可以用List。
另附问题链接:https://stackoverflow.com/questions/60744667/speed-test-among-set-list-and-tuple-in-python-gives-surprising-results
原文链接:https://blog.csdn.net/weixin_43849588/article/details/104958531
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。