Empty HashSet - Count Against Any

I'm just curious to know if HashSet hs empty or not. I am NOT interested in knowing exactly how many elements it contains.

So I could use this:

 bool isEmpty = (hs.Count == 0); 

... or that:

 bool isEmpty = hs.Any(x=>true); 

Which one provides the best results in terms of performance (especially when the HashSet contains a large number of elements)?

+4
source share
3 answers

In the HashSet, you can use both options, since the HashSet internally controls the counter.

However, if your data is in an IEnumerable<T> or IQueryable<T> result.Any() , using result.Any() preferable to result.Count() (both Linq methods).

Linq .Count() will iterate over the whole Enumerable, .Any() will only look if any objects exist in Enumerable or not.

Update: Just a small addition: In your case with a HashSet .Count may be preferable, since .Any() will require creating and returning an IEmumerator , which is a little overhead if you are not going to use Enumerator anywhere in your code ( foreach , Linq etc.). But I think this will be considered "Micro-optimization."

+11
source

HastSet<T> implements ICollection<T> that has the Count property, so calling Count() will just call HastSet<T>.Count , which I assume is an O (1) operation (this means t really needs count - it just returns the current size of the HashSet ).

Any will iterate until it finds an element matching the condition and then stops.

So, in your case, it just iterates over one element and then stops, so the difference is likely to be negligible .

If you have a filter that you want to apply (for example, x => x.IsValid ), then Any will definitely be faster, since Count(x => x.IsValid) will interact with the exentire collection, and Any will stop as soon as will find a match.

For these reasons, I usually prefer to use Any() rather than Count()==0 , as it is more direct and avoids any potential performance issues. I would switch to Count()==0 if it would provide a significant performance boost over Any() .

Note that Any(x=>true) logically matches the call to Any() . This does not change your question, but it looks cleaner without lambda.

+2
source

Depending on the type of collection, this may or may not matter in terms of performance. So, why not just use hs.Any() as it is designed specifically for what you need to know?

And the lambda expression x => true doesn't matter here. You can leave it.

0
source

Source: https://habr.com/ru/post/1497063/


All Articles