Jon*_*now 6 python performance intersection set data-structures
哪一个更快?一个"更好"吗?基本上我会有两套,我想最终从两个列表中得到一个匹配.所以我觉得for循环更像是:
for object in set:
if object in other_set:
return object
Run Code Online (Sandbox Code Playgroud)
就像我说的 - 我只需要一场比赛,但我不确定如何intersection()处理,所以我不知道它是否更好.此外,如果它有帮助,这other_set是一个近100,000个组件的列表,set可能是几百,最多几千.
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)
Run Code Online (Sandbox Code Playgroud)
时报:
>>>
0.0929054012768
0.637904308732
>>>
0.160845057616
1.08630760484
>>>
0.322059185123
1.10931801261
>>>
0.0758695262169
1.08920981403
>>>
0.247866360526
1.07724461708
>>>
0.301856152688
1.07903130641
Run Code Online (Sandbox Code Playgroud)
在设置中运行它们并运行10000次运行而不是100次运行
>>>
0.000413064976328
0.152831597075
>>>
0.00402408388788
1.49093627898
>>>
0.00394538156695
1.51841512101
>>>
0.00397715579584
1.52581949403
>>>
0.00421472926155
1.53156769646
Run Code Online (Sandbox Code Playgroud)
因此,无论是否将它们转换为集合,您的版本都会更快.
我意识到这是一篇较旧的帖子。但是,我来到这里寻找性能速度比较使用intersection与in并认为值得添加更多信息。上面的答案很好,但让我不清楚实际的最佳前进道路。
“第一个结果”解决方案并不能专门解决我的用例。
相反,我想知道不同的实现如何执行,如何使用离散的方法产生相同的结果集。不仅仅是第一个相交值。因此,下面我包含了通过 1000 次循环测试来执行选项评估的代码。与 @agf 发布的相反,当所需的输出是匹配列表时,使用集合要快得多。
我的结果是:
runForin took 132851.600ms
runForinBlist took 37700.916ms
True
runForInListComp took 132783.147ms
True
runForinSet took 780.919ms
True
runSetIntersection took 760.980ms (WINNER)
True
runSetin took 771.921ms
True
Run Code Online (Sandbox Code Playgroud)
这是代码。希望它能帮助某人。注意:我还评估了 blist ( http://stutzbachenterprises.com/blist/blist.html ) 库,因为它在其他用例中表现良好。
runForin took 132851.600ms
runForinBlist took 37700.916ms
True
runForInListComp took 132783.147ms
True
runForinSet took 780.919ms
True
runSetIntersection took 760.980ms (WINNER)
True
runSetin took 771.921ms
True
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5905 次 |
| 最近记录: |