I have been searching for a performance benchmarking between Contains
, Exists
and Any
methods available in the List<T>
. I wanted to find this out just out of curiosity as I was always confused among these. Many questions on SO described definitions of these methods such as:
- LINQ Ring: Any() vs Contains() for Huge Collections
- Linq .Any VS .Exists - Whats the difference?
- LINQ extension methods - Any() vs. Where() vs. Exists()
So I decided to do it myself. I am adding it as an answer. Any more insight on the results is most welcomed. I also did this benchmarking for arrays to see the results
According to documentation:
List.Exists (Object method)
IEnumerable.Any (Extension method)
List.Contains (Object Method)
Benchmarking:
CODE:
RESULTS
It is worth mentioning that this comparison is a bit unfair since the
Array
class doesn't own theContains()
method, it uses an extension method forIEnumerable<T>
via a sequentialEnumerator
hence it is not optimized forArray
instances; on the other side,HashSet<T>
has its own implementation fully optimized for all sizes.To compare fairly you could use the static method
int Array.IndexOf()
which is implemented forArray
instances even though it uses afor
loop slightly more efficient that anEnumerator
.Having said that, the performance of
HashSet<T>.Contains()
is similar to theArray.IndexOf()
for small sets of, I would say, up to 5 elements and much more efficient for large sets.The fastest way is to use a
HashSet
. TheContains
for aHashSet
is O(1).I took you code and added a benchmark for
HashSet<int>
The performance cost ofHashSet<int> set = new HashSet<int>(list);
is nearly zero.RESULTS