LINQ 环:大型集合的 Any() 与 Contains()
给定一个巨大的对象集合,以下对象之间是否存在性能差异?
myCollection.Contains(myElement)
Collection.Contains: microsoft.com/en-us/library/bb534972.aspx" rel="noreferrer">Enumerable.Any:
myCollection.Any(currentElement => currentElement == myElement)
Given a huge collection of objects, is there a performance difference between the the following?
myCollection.Contains(myElement)
myCollection.Any(currentElement => currentElement == myElement)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Contains()
是一个实例方法,其性能很大程度上取决于集合本身。例如,List
上的Contains()
的复杂度为 O(n),而HashSet
上的Contains()
的复杂度为 O(n)。是 O(1)。Any()
是一个扩展方法,将简单地遍历集合,将委托应用于每个对象。因此它的复杂度为 O(n)。然而,
Any()
更灵活,因为您可以传递委托。Contains()
只能接受一个对象。Contains()
is an instance method, and its performance depends largely on the collection itself. For instance,Contains()
on aList
is O(n), whileContains()
on aHashSet
is O(1).Any()
is an extension method, and will simply go through the collection, applying the delegate on every object. It therefore has a complexity of O(n).Any()
is more flexible however since you can pass a delegate.Contains()
can only accept an object.这取决于收藏。如果您有一个有序集合,那么
Contains
可能会进行智能搜索(二进制、散列、b 树等),而使用Any()
您基本上会陷入困境枚举直到找到它(假设 LINQ-to-Objects)。另请注意,在您的示例中,
Any()
使用==
运算符来检查引用相等性,而Contains
将使用IEquatable
或Equals()
方法,可能会被重写。It depends on the collection. If you have an ordered collection, then
Contains
might do a smart search (binary, hash, b-tree, etc.), while withAny()
you are basically stuck with enumerating until you find it (assuming LINQ-to-Objects).Also note that in your example,
Any()
is using the==
operator which will check for referential equality, whileContains
will useIEquatable<T>
or theEquals()
method, which might be overridden.我想这取决于
myCollection
的类型,它决定了Contains()
的实现方式。例如,如果是排序的二叉树,它可以更智能地进行搜索。它还可能考虑元素的哈希值。另一方面,Any()
将枚举集合,直到找到第一个满足条件的元素。如果对象具有更智能的搜索方法,则不会进行优化。I suppose that would depend on the type of
myCollection
is which dictates howContains()
is implemented. If a sorted binary tree for example, it could search smarter. Also it may take the element's hash into account.Any()
on the other hand will enumerate through the collection until the first element that satisfies the condition is found. There are no optimizations for if the object had a smarter search method.Contains() 也是一种扩展方法,如果您以正确的方式使用它,它可以快速工作。
例如:
var result = context.Projects.Where(x => lstBizIds.Contains(x.businessId)).Select(x => x.projectId).ToList();
这将给出查询
SELECT Id
来自项目
INNER JOIN (VALUES (1), (2), (3), (4), (5)) AS Data(Item) ON Projects.UserId = Data.Item
而另一方面 Any() 始终迭代 O(n)。
希望这会起作用......
Contains() is also an extension method which can work fast if you use it in the correct way.
For ex:
var result = context.Projects.Where(x => lstBizIds.Contains(x.businessId)).Select(x => x.projectId).ToList();
This will give the query
SELECT Id
FROM Projects
INNER JOIN (VALUES (1), (2), (3), (4), (5)) AS Data(Item) ON Projects.UserId = Data.Item
while Any() on the other hand always iterate through the O(n).
Hope this will work....