16

Results

Using a list of 10 million random ints (same seed each time, average of 10 repetitions):

listCopy.Sort(Comparer<int>.Default) takes 314ms.

Using

sealed class IntComparer : IComparer<int>
{
  public int Compare(int x, int y)
  {
    return x < y ? -1 : (x == y ? 0 : 1);
  }
}

listCopy.Sort(new IntComparer()) takes 716ms.

Some variations:

  • Using struct IntComparer instead of sealed class: 771ms
  • Using public int Compare(int x, int y) { return x.CompareTo(y); }: 809ms

Comments

Comparer<int>.Default returns a GenericComparer<int>. According to dotPeek, we have:

internal class GenericComparer<T> : Comparer<T> where T : IComparable<T>
{
  public override int Compare(T x, T y)
  {
    if ((object) x != null)
    {
      if ((object) y != null)
        return x.CompareTo(y);
      else
        return 1;
    }
    else
      return (object) y != null ? -1 : 0;
  }

...
}

Obviously, this shouldn't be faster than my IntComparer variant using CompareTo.

I didn't find anything relevant in ArraySortHelper<T>, which appears to be the core of List<T>.Sort.

I can only guess that the JIT does some magic special-casing here (Replace sorts which use Comparer<int>.Default by a specialized sorting implementation which doesn't do any IComparer<T>.Compare calls, or something similar)?

EDIT: The timings above are too low by a factor of 5.9214729782462845 (Stopwatch and TimeSpan have a different definition of "Tick"). Doesn't affect the point, though.

FunctorSalad
  • 2,502
  • 25
  • 20
  • 7
    Can we see the code that indicates how you actually timed it? A lot of "Why is X faster than Y?" questions have issues with how they were timed in the first place. – vcsjones Jul 11 '12 at 19:30
  • Are you running this test in debug or inside of the VS host process? – Reed Copsey Jul 11 '12 at 19:31
  • 4
    Also, if you change your IntComparer to return `x - y` how does that affect runtime? – Cameron Jul 11 '12 at 19:31
  • within your generic comparer, you are doing type checking, casting x to object for null check, and then calling a default comparer of the T you are passing it. why wouldnt it be slow. what s the real question here? – DarthVader Jul 11 '12 at 19:35
  • 1
    @DarthVader He is saying that the one that is doing the null check and using the default comparer is faster than his custom `IntComparer`. – vcsjones Jul 11 '12 at 19:38
  • @vcsjones: The actual code is longish, since I'm using a class I made for collecting time samples and calculating statistics. But essentially: I do one untimed warmup run first (for JIT), I use `StopWatch` for the timing, and I do an (untimed) `GC.Collect` before each repetition. – FunctorSalad Jul 11 '12 at 19:39
  • @ReedCopsey: No, I'm using "Start without debugging" in VS (Release build). – FunctorSalad Jul 11 '12 at 19:40
  • 2
    Try running them in a different order, see if you get the same results – Paul Phillips Jul 11 '12 at 19:40
  • 2
    @FunctorSalad Why are you doing the `GC.Collect`? GC.Collect isn't synchronous unless called with `GC.WaitForPendingFinalizers`. If you aren't waiting for finalizers, the collection may still be happening in the background of your timed code. – vcsjones Jul 11 '12 at 19:40
  • i can think that creating many IntComparer can cause that. why dont you jam it through ants profiler and see what s most expensive. – DarthVader Jul 11 '12 at 19:40
  • I think your assumption of what default comparer is running may not correct. Since Int32 implements IComparable, Comparer will set a static private var for the defaultComparer, and use that, and I think that default comparer directly executes the Int32.CompareTo(int,int), so no casts, etc. – hatchet - done with SOverflow Jul 11 '12 at 19:43
  • vcsjones: Trying to avoid one benchmark being disadvantaged by collecting garbage from the previous one. Didn't know that it isn't synchronous, thanks! – FunctorSalad Jul 11 '12 at 19:43
  • Are you creating a `new IntComparer()` in each loop? If so, try creating it once and pass that instance to `Sort` instead. – SuperOli Jul 11 '12 at 19:44
  • 2
    @Cameron returning x - y is not a good idea. You end up with overlow problems fairly easily; consider x = int.MinValue and y = int.MaxValue. You would end up returning a positive value instead of a negative value. – Monroe Thomas Jul 11 '12 at 19:51
  • SuperOli: No difference that way (726ms). – FunctorSalad Jul 11 '12 at 19:53
  • @PaulPhillips: Re: order, I ran each benchmark in a fresh process. The seed is `12345` (full list init code: `const int LEN = 10 * 1000 * 1000; const int SEED = 12345; var rnd = new Random(SEED); var list = new List(LEN); for (int i = 0; i < LEN; i++) list.Add(rnd.Next());` – FunctorSalad Jul 11 '12 at 19:59
  • @PaulPhillips how could the seed value affect the result? As long as the same seed value is being used so that the list to be sorted in each test is identical. – Monroe Thomas Jul 11 '12 at 20:00
  • @MonroeThomas it couldn't, I'm just trying to recreate it and wanted the same sequence of ints – Paul Phillips Jul 11 '12 at 20:01
  • 1
    I believe I got the same results: [this runs in linqpad](http://pastebin.com/kUyvPDFK). I was getting about ~2.5 seconds with the `IntComparer` and ~1.1 with the `Default`. – Paul Phillips Jul 11 '12 at 20:08
  • @vcsjones: There was indeed a bug in my timing (turned out that `watch.ElapsedTicks / TimeSpan.TicksPerMillisecond` is bogus because apparently there's a conversion factor `Stopwatch.tickFrequency` between `Stopwatch` ticks and `TimeSpan` ticks). I put the edit at the bottom though because it doesn't affect the point. Thanks @Paul Phillips for making me suspicious of the magnitude of my timings. – FunctorSalad Jul 11 '12 at 21:38

3 Answers3

24

The reason is readily visible in the Reference Source, system/array.cs source code file:

   [ReliabilityContract(Consistency.MayCorruptInstance, Cer.MayFail)]
   public static void Sort<T>(T[] array, int index, int length, System.Collections.Generic.IComparer<T> comparer) {
       // Argument checking code omitted
       //...

       if (length > 1) {
           // <STRIP>
           // TrySZSort is still faster than the generic implementation.
           // The reason is Int32.CompareTo is still expensive than just using "<" or ">".
           // </STRIP>
           if ( comparer == null || comparer == Comparer<T>.Default ) {
               if(TrySZSort(array, null, index, index + length - 1)) {
                   return;
               }
           }

           ArraySortHelper<T>.Default.Sort(array, index, length, comparer);
       }
   }

The comment marked by <STRIP> explains it, in spite of its broken English :) The code path for the default comparer goes through TrySZSort(), a function that's implemented in the CLR and written in C++. You can get its source code from SSCLI20, it is implemented in clr/src/vm/comarrayhelpers.cpp. It uses a template class method named ArrayHelpers<T>::QuickSort().

It gets the speed advantage from being able to use the < operator, a single cpu instruction instead of the 10 required by Int32.CompareTo(). Or in other words, IComparable<>.CompareTo is over-specified for simple sorting.

It is a micro-optimization, the .NET Framework has lots and lots of them. The inevitable fate of code that sits at the very bottom of a dependency chain, Microsoft can never assume that their code isn't going to be speed-critical in a customer's app.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • I feel stupid now. I actually skimmed over that method in dotPeek (not the reference source); that last `if ( comparer == null ...` is tucked away in a single line there and I read over it as just another input validation line :) – FunctorSalad Jul 11 '12 at 20:34
  • @HansPassant starting with .Net 4.5 `Array.Sort` uses IntroSort. So am I right that in this case we are not using `TrySZSort` and thus performance is not suffering from custom comparer usage? – Alex Zhukovskiy Feb 26 '16 at 13:00
  • @HansPassant well, I answer myself: no, IntroSort is applyed only when TrySZSort fails. – Alex Zhukovskiy Feb 26 '16 at 13:23
  • @HansPassant TrySZSort also implements IntroSort – Michal Franc Apr 16 '18 at 19:40
4

ILSpy decompiles thus:

    public override int Compare(T x, T y)
    {
        if (x != null)
        {
            if (y != null)
            {
                return x.CompareTo(y);
            }
            return 1;
        }
        else
        {
            if (y != null)
            {
                return -1;
            }
            return 0;
        }
    }

The null checks will always evaluate as true for a value type, so they will be optimized away; the end result will be

public override int Compare(T x, T y)
{
    return x.CompareTo(y);
}
phoog
  • 42,068
  • 6
  • 79
  • 117
  • That doesn't seem to explain why the `Comparer.Default` is *faster*. – FunctorSalad Jul 11 '12 at 20:10
  • @FunctorSalad I just timed `x.CompareTo(y);` vs `x < y ? -1 : (x == y ? 0 : 1);`. `CompareTo` is about twice as fast on average in release mode. Since `Comparer.Default` will optimize to `x.CompareTo(y);`, that is why it is about twice as fast as your custom comparer. Change your custom comparer to use `x.CompareTo(y);` and you should see equivalent performance. – Monroe Thomas Jul 11 '12 at 20:16
  • @FunctorSalad Try replacing your `IntComparer` implementation with this one, see if the difference disappears. – Paul Phillips Jul 11 '12 at 20:18
  • @FunctorSalad you're right; I posted that answer knowing that it was incomplete, because I didn't have time to look into it more fully. I also want to call attention to the guess in your question ("I can only guess that the JIT does some magic special-casing here (Replace sorts which use Comparer.Default by a specialized sorting implementation which doesn't do any IComparer.Compare calls, or something similar)?"), which is obviously correct, except for the fact that the special casing is done in the BCL, not by the JIT. – phoog Jul 11 '12 at 23:14
1

The default comparer for Int32 is the CompareTo(int,int) method. Your assumption of the default comparer is incorrect.

The IComparable interface provides a strongly typed comparison method for ordering members of a generic collection object. Because of this, it is usually not called directly from developer code. Instead, it is called automatically by methods such as List.Sort() and Add.

http://msdn.microsoft.com/en-us/library/4d7sx9hd.aspx. The IComparable interface mentioned defines the CompareTo method.

So we should expect your comparer to be about the same speed. So why might it be slower? If we dig down into the Sort method in .Net, we eventually get to this line:

if ((length > 1) && (((comparer != null) && (comparer != Comparer<T>.Default)) || !TrySZSort(array, null, index, (index + length) - 1)))
{
    ArraySortHelper<T>.Default.Sort(array, index, length, comparer);
}

If the comparer equals the default comparer for that type, the Array Sort will try to use an internal optimized sort method. Your comparer is not the default comparer, so it skips that optimized sort.

  • Sorry, your edit is the correct answer, but I chose Hans Passant's answer because it was slightly earlier than the edit. Regarding your first point, I don't see my mistake here, as I concluded that `Comparer.Default` is a `GenericComparer`, which calls the `CompareTo` method from `ICompareble`. – FunctorSalad Jul 11 '12 at 21:08
  • That's fine, he posted while I was working on that edit, but didn't see his until after I saved my edit. His answer explains it well (I upvoted it too). I will add something to my post regarding the first point to explain better. – hatchet - done with SOverflow Jul 11 '12 at 21:11