Using Linq on collections, what is the difference between the following lines of code?
if(!coll.Any(i => i.Value))
and
if(!coll.Exists(i => i.Value))
Update 1
When I disassemble .Exists
it looks like there is no code.
Update 2
Anyone know why there is not code there for this one?
Additionally, this will only work if Value is of type bool. Normally this is used with predicates. Any predicate would be generally used find whether there is any element satisfying a given condition. Here you're just doing a map from your element i to a bool property. It will search for an "i" whose Value property is true. Once done, the method will return true.
As a continuation on Matas' answer on benchmarking.
TL/DR: Exists() and Any() are equally fast.
First off: Benchmarking using Stopwatch is not precise (see series0ne's answer on a different, but similiar, topic), but it is far more precise than DateTime.
The way to get really precise readings is by using Performance Profiling. But one way to get a sense of how the two methods' performance measure up to each other is by executing both methods loads of times and then comparing the fastest execution time of each. That way, it really doesn't matter that JITing and other noise gives us bad readings (and it does), because both executions are "equally misguiding" in a sense.
After executing the above code 4 times (which in turn do 1 000
Exists()
andAny()
on a list with 1 000 000 elements), it's not hard to see that the methods are pretty much equally fast.There is a slight difference, but it's too small a difference to not be explained by background noise. My guess would be that if one would do 10 000 or 100 000
Exists()
andAny()
instead, that slight difference would disappear more or less.TLDR; Performance-wise
Any
seems to be slower (if I have set this up properly to evaluate both values at almost same time)testing list generator:
With 10M records
With 5M records
With 1M records
With 500k, (I also flipped around order in which they get evaluated to see if there is no additional operation associated with whichever runs first.)
With 100k records
It would seem
Any
to be slower by magnitude of 2.Edit: For 5 and 10M records I changed the way it generates the list and
Exists
suddenly became slower thanAny
which implies there's something wrong in the way I am testing.New testing mechanism:
Edit2: Ok so to eliminate any influence from generating test data I wrote it all to file and now read it from there.
10M
5M
1M
500k
When you correct the measurements - as mentioned above: Any and Exists, and adding average - we'll get following output:
See documentation
List.Exists (Object method - MSDN)
This exists since .NET 2.0, so before LINQ. Meant to be used with the Predicate delegate, but lambda expressions are backward compatible. Also, just List has this (not even IList)
IEnumerable.Any (Extension method - MSDN)
This is new in .NET 3.5 and uses Func(TSource, bool) as argument, so this was intended to be used with lambda expressions and LINQ.
In behaviour, these are identical.
The difference is that Any is an extension method for any
IEnumerable<T>
defined on System.Linq.Enumerable. It can be used on anyIEnumerable<T>
instance.Exists does not appear to be an extension method. My guess is that coll is of type
List<T>
. If so Exists is an instance method which functions very similar to Any.In short, the methods are essentially the same. One is more general than the other.