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

后端 未结 5 1271
时光说笑
时光说笑 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

    LINQ heavily uses deferred execution. Unless you enumerate a query, it does not get executed.

    Change

     s.Start();
     z= a.Intersect(b);
     s.Stop();
    

    to

     s.Start();
     z= a.Intersect(b).**ToArray**();
     s.Stop();
    

    and please post the new performance results.

    a.Intersect(b) represents an expression, independent of the value of a and b. Values of a and b are only used when the expression is evaluated by enumeration.

提交回复
热议问题