Reviewing the framework... it depends. My initial instincts in hypothetical-land:
Any()
checks to see if there is a single value. If so, then it
returns true. This is an O(1) operation.
Count()
would have to either do one of the following:
1) access a running tally of items in the collection, or 2) count
the items in the collection
In the best case (#1) the operation is O(1). In the worst, (#2), it's
O(n).
In reality, Any()
uses the collection's iterator to determine if there is a next value. So, it is up to the collection whether or not Any()
is an O(1) operation. If it is a poor implementation, it could possibly be O(n).
For example, let's say the Array iterator is stupid, and looks for the first non-null value. It has to examine each item in the array, and therefore Any()
in this case means O(n). (In fact, Any()
returns true
for any array of length > 1).
Count()
attempts to see if the collection implements ICollection
or ICollection<T>
, and if so, returns the Count
property's value. If the underlying implementation keeps a running tab, that could be O(1). If not, it could be O(n) worst case.
If the enumerable doesn't implement one of those interfaces, Count()
simply iterates the entire collection, counting on the way. That's O(n).
tl;dr: according to the implementation, Any()
is more likely to be much faster than Count()
.