在元素比对中,可以采用intersection或index操作,这次我们沿用上一次比对的模型进行性能验证
tps=0
while tps <5 :
tps +=1
#列表查找检测
import time
list=[1,2,3,4,5,6,7]
list1=[1,2,3,4,5,6,7,8,9,10,11,12,13,14]
'''start = time.time()
k=0
while k < 10000:
k+=1
for i in list1:
for n in list:
if i == n :
object_listnnnn = i
else:
object_listnnnn = i
end = time.time()
print((end - start))'''
start = time.time()
list=set(list)
list1=set(list1)
k=0
while k < 10000:
k+=1
z = list1.intersection(list)
end = time.time()
print((end - start))
start = time.time()
k=0
while k < 10000:
k+=1
for i in list1:
try:
object_listnnnn = list.index(i)
object_listnnnn = i
except:
object_listnnnn = i
end = time.time()
print((end - start))
'''
start = time.time()
k=0
while k < 10000:
k+=1
for i in list1:
for n in list:
try:
object_listnnnn = n.index(i)
object_listnnnn = i
except:
object_listnnnn = i
end = time.time()
print((end - start))'''
这次输出的结果
交集完胜,但是交集有诸多约束,需要按需使用
1 | 2 | 3 | 4 | 5 | 平均 | s |
0.02 | 0.03 | 0.02 | 0.02 | 0.02 | 0.02 | 交集 |
0.49 | 0.36 | 0.41 | 0.37 | 0.38 | 0.40 | index |