while looking at this question C# Similarities of two arrays it was noted that the initial linq call was significantly slower than subsequent calls. What is being cached th
Enumerable.Intersect
does not do any caching. It is implemented using a HashSet
. The first sequence is added to the HashSet
. Then the second sequence is removed from the HashSet
. The remaining elements in the HashSet
is yielded as an enumerable sequence of elements. You will have to actually enumerate the HashSet
to pay the cost of creating the HashSet
. This implementation is suprisingly efficient even for small collections.
If you see a difference in performance in subsequent calls it is not because Enumerable.Intersect
does any caching but probably because you need to "warm up" your benchmark.