in linq why are subsequent calls of IEnumerable.Intersect so much faster

后端 未结 5 1269
时光说笑
时光说笑 2021-01-19 18:10

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

5条回答
  •  再見小時候
    2021-01-19 18:53

    You are enumerating the result of Intersect() only when you call Count(); that's when the calculation of the intersection actually occurs. The part you're timing is the creation of the enumerable object that represents the future calculation of the intersection.

    In addition to the jitting penalty others have noted, the first call to Intersect() might be the first use of a type from System.Core.dll, so you might be looking at the time required to load the IL code into memory, as well.

提交回复
热议问题