哪个更快,为什么?设置还是列表?
假设我有一个图表,想查看 N[a] 中是否有 b
。哪个实施速度更快,为什么?
a, b = range(2)
N = [set([b]), set([a,b])]
或者
N= [[b],[a,b]]
这显然过于简单化了,但想象一下图变得非常密集。
Lets say that I have a graph and want to see if b in N[a]
. Which is the faster implementation and why?
a, b = range(2)
N = [set([b]), set([a,b])]
OR
N= [[b],[a,b]]
This is obviously oversimplified, but imagine that the graph becomes really dense.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
集合中的成员资格测试要快得多,尤其是对于大型集合。这是因为该集合使用哈希函数来映射到存储桶。由于 Python 实现会自动调整哈希表的大小,因此速度可以是恒定的 (
O(1)
)无论集合的大小如何(假设哈希函数足够好)。相反,为了评估一个对象是否是列表的成员,Python 必须比较每个成员是否相等,即测试时间为
O(n)
。Membership testing in a set is vastly faster, especially for large sets. That is because the set uses a hash function to map to a bucket. Since Python implementations automatically resize that hash table, the speed can be constant (
O(1)
) no matter the size of the set (assuming the hash function is sufficiently good).In contrast, to evaluate whether an object is a member of a list, Python has to compare every single member for equality, i.e. the test is
O(n)
.这完全取决于您想要实现的目标。逐字使用示例,使用列表会更快,因为您不必花费创建集合的开销:
生成:
但是,由于此处已经提到的原因,当您搜索<时,您可以从使用集合中受益/em> 大集合。通过你的例子不可能判断拐点对你来说在哪里以及你是否会看到好处。
我建议您对这两种方法进行测试,并选择适合您的特定用例的更快的方法。
It all depends on what you're trying to accomplish. Using your example verbatim, it's faster to use lists, as you don't have to go through the overhead of creating the sets:
Produces:
However, for reasons already mentioned here, you benefit from using sets when you are searching large sets. It's impossible to tell by your example where that inflection point is for you and whether or not you'll see the benefit.
I suggest you test it both ways and go with whatever is faster for your specific use-case.
Set(我的意思是基于哈希的集合,如 HashSet)比 List 查找值要快得多。列表必须按顺序查找该值是否存在。 HashSet可以直接跳转定位桶,几乎在常数时间内查找到一个值。
Set ( I mean a hash based set like HashSet) is much faster than List to lookup for a value. List has to go sequentially to find out if the value exists. HashSet can directly jump and locate the bucket and look up for a value almost in a constant time.