-6
public void MethodName(ObservableCollection<DataCollection> dataCollection)
    {
        if (dataCollection != null)
        {
            IsChecked = dataCollection.Any(o => o.DataCollectionID.Equals(30));

            IsChecked = dataCollection.Where(o => o.DataCollectionID.Equals(30)).Count() > 0;
        }            
    }

Can anyone explain me, what could be the most efficient way to use from above two filtering? .Any? or .Where.Count?

Note: Consider that dataCollection has over 10,000 items.

Please advice me. Thank you

Rosh
  • 55
  • 6
  • 2
    My advice is to test it yourself. – Zohar Peled Aug 19 '15 at 15:01
  • 5
    With `Any` as soon as a condition is met the method terminates. With `Count` it has to go all the way to the end. This is really something you should have been able to find out with a little research. – Daniel Kelley Aug 19 '15 at 15:01
  • `over 10,000 items` that's not much. sounds like micro optimization to me. is this even your bottleneck? what does your profiling say? if you want to micro optimize just for the sake of it, then skip LINQ all together. or at least just PLINQ. – Num Lock Aug 19 '15 at 15:06

1 Answers1

1

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

  • 2
    I believe number 1 would be O(1) in the best case, but O(n/2) (so O(n)) in the average case. Number 2 is always O(n). Obviously number 1 is generally more efficient, but it's definitely not O(1) (constant time) every time. – johnnyRose Aug 19 '15 at 15:05
  • @johnnyRose In the average case? (FYI, I'm checking the source now)... That would be a terrible design. Are you thinking about one item at the end of an array? Yeah, I can see that. BRB. –  Aug 19 '15 at 15:07
  • `O(1)` for `Any` would be the best case scenario. Not always. – Habib Aug 19 '15 at 15:10
  • @Habib yep. Checking the source helps. –  Aug 19 '15 at 15:23