21

Which is faster: Union or Concat?

I don't care about the order of the elements.

Enumerable.Union Method

Enumerable.Concat Method

Eliahu Aaron
  • 4,103
  • 5
  • 27
  • 37
Jader Dias
  • 88,211
  • 155
  • 421
  • 625
  • 11
    Try it both ways. Get out a stopwatch. Then you'll know. Performance "analysis" based on *guesswork*, no matter how educated, is not actually useful as a basis to make *engineering* decisions. – Eric Lippert Aug 26 '09 at 22:01

3 Answers3

56

Union removes duplicates. Concat does not.

So, they produce different results if the sources either contain any items in common, or have any internal duplicates.

If you can guarantee there are no duplicates, or if there are few and you don't care about having them in your output, Concat will be faster since there's no need to test each value against what has already been yielded.

However, if there are many duplicates and you don't need them, the extra processing in Union to remove the dupes may be offset by the savings in your code that consumes the results.

Eliahu Aaron
  • 4,103
  • 5
  • 27
  • 37
richardtallent
  • 34,724
  • 14
  • 83
  • 123
10

Do you only care about execution speed? How long does it take you to process an element when you receive it?

Concat is simpler - it doesn't need to perform any processing itself, or buffer the results that it's already returned. However, it will produce more results if there are any elements in the intersection. If you're going to take a long time to process each result, Concat may end up effectively being slower.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • In my case, I'll use a Distinct() in the end, which favors the use of Union I think. – Jader Dias Aug 26 '09 at 21:54
  • 6
    If you use Union then you don't need to call Distinct afterwards anyway. – Jon Skeet Aug 26 '09 at 21:57
  • 2
    Union removes duplicates between the lists, but if the first list has duplicates within itself those will not be removed by union. So - Distinct might still need to be called, depending on circumstances. – Amy B Aug 27 '09 at 12:24
  • 12
    @DavidB: That's not true (i'm not sure if it was sometime). `Union` will remove dups even if they are duplicate only in one of both lists. – Tim Schmelter Jun 01 '12 at 10:40
  • @TimSchmelter, Confirmed! (with a quick console app) :) – nurchi Oct 13 '16 at 21:12
3

What was said above is right. Here is just a little addition for some special cases:

If you have to concatenate for example two lists and if you need full speed, consider using yield. Of course this is a lot less flexible and comfortable than Union or Concat in Linq. Therefore it makes only sense in special cases.

This property for example will provide the same as List1.Concat(List2)

public IEnumerable<MyObject> AllObjects
{
    get
    {
        foreach (MyObject o1 in List1)
            yield return o1;

        foreach (MyObject o2 in List2)
            yield return o2;
    }
}
Eliahu Aaron
  • 4,103
  • 5
  • 27
  • 37
Knasterbax
  • 7,895
  • 1
  • 29
  • 23
  • `Concat` exactly does it the way you show it in the example (see: [source](https://referencesource.microsoft.com/#System.Core/System/Linq/Enumerable.cs#800)). So there is no need to implement it by your own. – scher Nov 08 '17 at 10:21