1

I'm matching some in memory lists entities with a .contains (subselect) query to filter out old from new users.

Checking for performance problems i saw this:

enter image description here

The oldList mostly has around 1000 users in them, while the new list varies from 100 to 500. Is there a way to optimize this query?

2
  • 1
    1 for 12 on accepted answers. Might want to improve upon that. Also, what add-on is giving you that metric? Commented Jan 23, 2012 at 16:41
  • I used the performance profiler of vs2010, it comes with the ultimate version. Commented Jan 23, 2012 at 18:13

3 Answers 3

3

Absolutely - build a set instead of checking a list each time:

// Change string to whatever the type of UserID is.
var oldUserSet = new HashSet<string>(oldList.Select(o => o.UserID));
var newUsers = NewList.Where(n => !oldUserSet.Contains(n.UserID))
                      .ToList();

The containment check on a HashSet should be O(1) assuming few hash collisions, instead of the O(N) of checking each against the whole sequence (for each new user).

Sign up to request clarification or add additional context in comments.

Comments

2

You could make a HashSet<T> of your user IDs in advance. This will cause the Contains to become an O(1) operation:

var oldSet = new HashSet<int>(oldList.Select(o => o.UserID));
var newUsers = NewList.Where(n => !oldSet.Contains(n.UserID)).ToList();

Comments

0

While those HashSet<T> answers are straightforward and simple, some may prefer a linq-centric solution.

LinqToObjects implements join and GroupJoin with a HashSet. Just use one of those - this example uses GroupJoin:

List<User> newUsers =
  (
    from n in NewList
    join o in oldList on n.UserId equals o.UserId into oldGroup
    where !oldGroup.Any()
    select n
  ).ToList()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.