LINQ 环:大型集合的 Any() 与 Contains()

发布于 2024-10-07 14:40:54 字数 460 浏览 0 评论 0原文

给定一个巨大的对象集合,以下对象之间是否存在性能差异?

Collection.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?

Collection.Contains:

myCollection.Contains(myElement)

Enumerable.Any:

myCollection.Any(currentElement => currentElement == myElement)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

后知后觉 2024-10-14 14:40:54

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 a List is O(n), while Contains() on a HashSet 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.

笨笨の傻瓜 2024-10-14 14:40:54

这取决于收藏。如果您有一个有序集合,那么 Contains 可能会进行智能搜索(二进制、散列、b 树等),而使用 Any() 您基本上会陷入困境枚举直到找到它(假设 LINQ-to-Objects)。

另请注意,在您的示例中, Any() 使用 == 运算符来检查引用相等性,而 Contains 将使用 IEquatableEquals() 方法,可能会被重写。

It depends on the collection. If you have an ordered collection, then Contains might do a smart search (binary, hash, b-tree, etc.), while with Any() 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, while Contains will use IEquatable<T> or the Equals() method, which might be overridden.

飘逸的'云 2024-10-14 14:40:54

我想这取决于 myCollection 的类型,它决定了 Contains() 的实现方式。例如,如果是排序的二叉树,它可以更智能地进行搜索。它还可能考虑元素的哈希值。另一方面,Any() 将枚举集合,直到找到第一个满足条件的元素。如果对象具有更智能的搜索方法,则不会进行优化。

I suppose that would depend on the type of myCollection is which dictates how Contains() 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.

挽心 2024-10-14 14:40:54

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....

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文