HashSet およびLinqクエリのパフォーマンス

During last week I received some code and was asked to improve
the performance. So started with the job, but soon I saw that they
use a lot of HashSet objects, to store big collections
of objects (between 10000 to more than 100000 objects). In the code
they use HashSet for performance reasons.

HashSetに参加しているか、 First()または

I’m wondering if we gain any performance advantage compared to a
normal List? Because all the Linq extension methods
they use in code are written for IEnumerable.





If you use just LINQ queries, you don’t get any perf advantage,
since you are just enumerating through the entire collection. In
fact, it could be that List is better performance
because of the contiguous internal storage.

To get the perf benefit of a HashSet, you need to
use the ISet methods, ideally with another
HashSet since, looking at the code, it is optimized
for this case. Further, operations will only be faster which take
advantage of the member objects’ hash codes, like equality testing,
since the performance of HashSet is based on the O(1)
performance characteristic of hash lookups. Operations which don’t
make use of the members’ hash codes, like filtering on a member
property vs. the members themselves, will need to be an O(N)
operation, making it the same as a List.


メールアドレスが公開されることはありません。 * が付いている欄は必須項目です