14

I have the classes:

class SomeClass
{
   public string Name{get;set;}
   public int SomeInt{get;set;}
}


class SomeComparison: IEqualityComparer<SomeClass>
{
     public bool Equals(SomeClass s, SomeClass d)
     {
         return s.Name == d.Name;
     }

     public int GetHashCode(SomeClass a)
     {
         return (a.Name.GetHashCode() * 251);
     }
}

I also have two large List<SomeClass> called list1 and list2

before I used to have:

 var q = (from a in list1
         from b in list2
         where a.Name != b.Name
         select a).ToList();

and that took about 1 minute to execute. Now I have:

var q =  list1.Except(list2,new SomeComparison()).ToList();

and that takes less than 1 second!

I will like to understand what does the Except method do. Does the method creates a hash table of each list and then perform the same comparison? If I will be performing a lot of this comparisons should I create a Hashtable instead?


EDIT

Now instead of having lists I have two HashSet<SomeClass> called hashSet1 and hashSet2

when I do:

   var q = (from a in hashSet1
           form b in hashSet2
           where a.Name != b.Name
           select a).ToList();

that still takes a long time... What am I doing wrong?

Tono Nam
  • 34,064
  • 78
  • 298
  • 470

5 Answers5

29

Your guess was close - the Linq to Objects Except extension method uses a HashSet<T> internally for the second sequence passed in - that allows it to look up elements in O(1) while iterating over the first sequence to filter out elements that are contained in the second sequence, hence the overall effort is O(n+m) where n and m are the length of the input sequences - this is the best you can hope to do since you have to look at each element at least once.

For a review of how this might be implemented I recommend Jon Skeet's EduLinq series, here part of it's implementation of Except and the link to the full chapter:

private static IEnumerable<TSource> ExceptImpl<TSource>(
    IEnumerable<TSource> first,
    IEnumerable<TSource> second,
    IEqualityComparer<TSource> comparer)
{
    HashSet<TSource> bannedElements = new HashSet<TSource>(second, comparer);
    foreach (TSource item in first)
    {
        if (bannedElements.Add(item))
        {
            yield return item;
        }
    }
}

Your first implementation on the other hand will compare each element in the first list to each element in the second list - it is performing a cross product. This will require nm operations so it will run in O(nm) - when n and m become large this becomes prohibitively slow very fast. (Also this solution is wrong as is since it will create duplicate elements).

dnickless
  • 10,733
  • 1
  • 19
  • 34
BrokenGlass
  • 158,293
  • 28
  • 286
  • 335
  • Okay, but why is that so much faster than the first option? – svick Apr 22 '12 at 16:24
  • I think cause it does not have to iterate through the whole list2 again in order to make the comparison. When adding if there is a collision it does not add the results... – Tono Nam Apr 22 '12 at 16:26
  • BrokenGlass if I will be performing a lot of this comparisons should I create a HashSet instead? – Tono Nam Apr 22 '12 at 16:29
  • @TonoNam: Yes - use a HashSet, also I don't think your current approach - despite being slow - will produce the desired results - it will produce duplicates. See updated answer. – BrokenGlass Apr 22 '12 at 16:33
  • BrokenGlass I am working on an update... thanks a lot for the help. – Tono Nam Apr 22 '12 at 16:35
  • 1
    If you want to be nitpicky, internally they use a custom implementation of a hash set. They don't use the one included in the framework. I suppose to keep it lightweight with a less bloated version. – Jeff Mercado Apr 22 '12 at 16:37
2

The two code examples do not produce the same results.

Your old code creates the Cartesian Product of the two lists.

That means it will return each element a in list1 multiple times - once for each element b in list2 that is not equal to a.

With "large" lists, this will take a long time.

Nick Butler
  • 24,045
  • 4
  • 49
  • 70
2

from a in list1 from b in list2 creates list1.Count * list2.Count elements and is not the same as list1.Except(list2)!

If list1 has the elements { a, b, c, d } and list2 the elements { a, b, c }, then your first query will yield the following pairs:

(a,a), (a,b), (a,c),  
(b,a), (b,b), (b,c),  
(c,a), (c,b), (c,c),  
(d,a), (d,b), (d,c)

because you exclude equal items the result will be

(a,a), (a,b), (a,c),  
(b,a), (b,b), (b,c),  
(c,a), (c,b), (c,c),  
(d,a), (d,b), (d,c)

And because you select only the first element of the pairs, you will get

{ a, a, b, b, c, c, d, d, d }


The second query will yield { a, b, c, d } minus { a, b, c }, i.e { d }.


If no hash table is was used in Exclude then a nested loop performing with O(m*n) would result. With a hash table the query approximately performs with O(n) (neglecting the cost for filling the hash table).

Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
0

This is the way i think about it.

IEnumerable<T> Except<T>(IEnumerable<T> a,IEnumerable<T> b)
{
    return a.Where(x => !b.Contains(x)).Distinct();
}
Rezo Megrelidze
  • 2,877
  • 1
  • 26
  • 29
0

It seems to me this would be more efficient

private static IEnumerable<TSource> ExceptImpl<TSource>(
    IEnumerable<TSource> first,
    IEnumerable<TSource> second,
    IEqualityComparer<TSource> comparer)
{
    HashSet<TSource> bannedElements = new HashSet<TSource>(second, comparer);
    foreach (TSource item in first)
    {
        if (!bannedElements.Contains(item))
        {
            yield return item;
        }
    }
}

Contains is O(1)

Add is if Count is less than the capacity of the internal array, this method is an O(1) operation. If the HashSet object must be resized, this method becomes an O(n) operation, where n is Count.

Ferhat Sayan
  • 216
  • 1
  • 4
  • 19
paparazzo
  • 44,497
  • 23
  • 105
  • 176