DISQUS

Emad Ibrahim: Improve LINQ Query Performance

  • Ed Richard · 1 year ago
    I'm wondering Emad, have you considered optimizing using the new GetTable method on the folder class?

    Ed
  • Emad Ibrahim · 1 year ago
    Ed, thanks for the tip, I will give it a shot and report back.
  • fdumlao · 8 months ago
    Essentially what you were looking for here was the difference of two collections (myTasks and outlookTasks). Enumerable has a built in method (Except()) to handle these when the collections are of the same type, and LINQ adds the ability to pass in a query to the "Except" method so that you can transform one collection before it is used.

    Your first approach was O(N^2), iterating over one collection then iterating over the second collection once for each iteration of the first collection.

    Your second approach is better: O(N) + O(N), which will iterate through collection one, then collection 2. However as the collections get bigger this will begin to take longer, which an optimized intersect algorithm can help you to avoid!

    if you instead used:

    Dim results = _
    myTasks.Except(From outlookTask In outlookTasks _
    Select outlookTask.Subject)

    which I believe is implemented as O(N log N) which should nicely plateau and provide reliable performance even as the collections get larger, not to mention fewer lines of code!

    Hope this helped!