交点()和'对象中的对象在集合中的速度差异如果对象在other_set' [英] Speed differences between intersection() and 'object for object in set if object in other_set'
问题描述
其中哪一个更快?是一个更好吗?基本上我会有两套,我想最终从两个列表之间获得一个匹配。所以真的假设for循环更像:
Which one of these is faster? Is one "better"? Basically I'll have two sets and I want to eventually get one match from between the two lists. So really I suppose the for loop is more like:
for object in set:
if object in other_set:
return object
像我说的 - 我只需要一个匹配,但我不确定如何处理 intersection()
,所以我不知道它是否更好。另外,如果它有帮助, other_set
是100,000个组件附近的列表,集合
可能是几百, em> max 几千。
Like I said - I only need one match, but I'm not sure how intersection()
is handled, so I don't know if its any better. Also, if it helps, the other_set
is a list near 100,000 components and the set
is maybe a few hundred, max few thousand.
推荐答案
from timeit import timeit
setup = """
from random import sample, shuffle
a = range(100000)
b = sample(a, 1000)
a.reverse()
"""
forin = setup + """
def forin():
# a = set(a)
for obj in b:
if obj in a:
return obj
"""
setin = setup + """
def setin():
# original method:
# return tuple(set(a) & set(b))[0]
# suggested in comment, doesn't change conclusion:
return next(iter(set(a) & set(b)))
"""
print timeit("forin()", forin, number = 100)
print timeit("setin()", setin, number = 100)
Times:
>>>
0.0929054012768
0.637904308732
>>>
0.160845057616
1.08630760484
>>>
0.322059185123
1.10931801261
>>>
0.0758695262169
1.08920981403
>>>
0.247866360526
1.07724461708
>>>
0.301856152688
1.07903130641
使其成为设置并运行10000次运行而不是100个收益
Making them into sets in the setup and running 10000 runs instead of 100 yields
>>>
0.000413064976328
0.152831597075
>>>
0.00402408388788
1.49093627898
>>>
0.00394538156695
1.51841512101
>>>
0.00397715579584
1.52581949403
>>>
0.00421472926155
1.53156769646
所以你的版本要快得多吗感觉将它们转换成集合。
So your version is much faster whether or not it makes sense to convert them to sets.
这篇关于交点()和'对象中的对象在集合中的速度差异如果对象在other_set'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!