Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ Ring: Any() vs Contains() for Huge Collections

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) 
like image 503
SDReyes Avatar asked Dec 14 '10 23:12

SDReyes


People also ask

What is the difference between contains and any in Linq?

Contains takes an object, Any takes a predicate. So if you want to check for a specific condition, use Any . If you want to check for the existence of an element, use Contains .

What is any () in Linq?

The Any operator is used to check whether any element in the sequence or collection satisfy the given condition. If one or more element satisfies the given condition, then it will return true. If any element does not satisfy the given condition, then it will return false.


2 Answers

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.

like image 155
Etienne de Martel Avatar answered Oct 14 '22 17:10

Etienne de Martel


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.

like image 44
tster Avatar answered Oct 14 '22 17:10

tster