LINQ Except方法是否自带Distinct?大数据量下性能优化咨询
First, to clarify: Enumerable.Except already returns distinct elements from the first sequence that are not present in the second. So the output of list1.Except(list2) is identical to list1.Distinct().Except(list2)—both will give you unique values from list1 that aren’t in list2.
Now, for performance with large datasets (500k+ records):
If list1 has many duplicate values, calling
Distinct()first will almost certainly be faster. Here’s why:Exceptinternally uses two hash sets: one to track elements from list2, and another to track elements it’s already added to the result. For every duplicate in list1, it still has to check both sets (even though the duplicate won’t be added to the result).- By running
Distinct()first, you eliminate all duplicates upfront, reducing the number of elementsExceptneeds to process. This cuts down on redundant set checks, which adds up with large numbers of duplicates.
If list1 has very few duplicates, the difference may be negligible—or
list1.Except(list2)could be slightly faster. The extraDistinct()call adds an additional pass through list1 and an extra hash set operation, which might outweigh any savings from reducing duplicate checks inExcept.
Since your example shows significant duplicates (like multiple 1s and 7s), list1.Distinct().Except(list2) is likely the better choice for your scenario. That said, the only way to be 100% sure is to benchmark both approaches with your actual data—performance can vary based on the exact distribution of duplicates and the size of list2.
内容的提问来源于stack exchange,提问作者BeNdErR

