Home > Back-end >  Are C# Set Operations (Union, Intersect, Except, DIstinct) faster if the underlying collection is a
Are C# Set Operations (Union, Intersect, Except, DIstinct) faster if the underlying collection is a

Time:10-21

For a large scale Set operations that I've been doing using List<T> and Union<T>, Intersect<T>.

Instead of List<T> to perform these LINQ Set Operations, could I just swap the underlying collection to a HashSet<T> and immediately see performance improvements?

CodePudding user response:

Those methods are all smart enough to use O(n) algorithms if the underlying collection doesn't already implement the set operations in an interface, so the performance difference isn't enough to worry about for most use cases.

There's a roughly 10% difference in my LINQPad-based benchmarks of each of those methods.

  • Related