IEnumerable Performance Tip: Any() vs. Count()

I can’t count the number of times (unintentional pun) I’ve checked if an IEnumerable<T> sequence contains elements using Count().

static void Method(IEnumerable<Status> statuses)  
    if (statuses != null && statuses.Count() > 0)  
        // do something...  

To get the count, the code has to traverse the entire sequence. On a long, lazy-executed sequences, this can take significant time. Since I only want to know if the sequence contains one or more elements, it’s computationally more efficient to use the Any() extension method.

static void Method(IEnumerable<Status> statuses)  
    if (statuses != null && statuses.Any())  
        // do something...  

In this case, Any() will return after examining the first element in the sequence. It also reads a better (IMHO).

← newer older →
.Net, Technology, Life, Whatever

Recent Posts

Tweetz 2.0.0 Released
Tweetz 2.0 Beta
VSColorOutput 2.7 - Time Stamps
Fixed Focal-Length Eyeglasses, a Programmer's Best Friend
How to Choose the Right VPN Service
Two Handy Command Line Scripts
Tweetz 1.3.2
More... (1088)