4

I have written the code testing speed of sorting methods. It Generates a Collection and sorts it using different methods.

public void TestMethod1()
    {
        var unsortedCollection = GenerateCollection();

        var toSort = unsortedCollection.ToList();
        Console.WriteLine("OrderBy x.A: " + Measure(() => toSort.OrderBy(x => x.A).ToArray()) + "ms");
        toSort = unsortedCollection.ToList();
        Console.WriteLine("OrderBy x: " + Measure(() => toSort.OrderBy(x => x).ToArray()) + "ms");
        toSort = unsortedCollection.ToList();
        Console.WriteLine("OrderBy x using Comparer: " + Measure(() => toSort.OrderBy(x => x, new Comparer()).ToArray()) + "ms");
        toSort = unsortedCollection.ToList();
        Console.WriteLine("OrderBy x using Comparer<int>.Default: " + Measure(() => toSort.OrderBy(x => x.A, Comparer<int>.Default).ToArray()) + "ms");
        toSort = unsortedCollection.ToList();
        Console.WriteLine("OrderBy x using Comparer<SimpleObject>.Default: " + Measure(() => toSort.OrderBy(x => x, Comparer<SimpleObject>.Default).ToArray()) + "ms");

        toSort = unsortedCollection.ToList();
        Console.WriteLine("Sort: " + Measure(() =>
                                                 {
                                                     toSort.Sort();
                                                     toSort.ToArray();
                                                 }) + "ms"); 
        toSort = unsortedCollection.ToList();
        Console.WriteLine("Sort using Comparison: " + Measure(() =>
                                                                  {
                                                                      toSort.Sort((x, y) => x.A.CompareTo(y.A));
                                                                      toSort.ToArray();
                                                                  }) + "ms");
        toSort = unsortedCollection.ToList();
        Console.WriteLine("Sort using Comparer: " + Measure(() =>
                                                                {
                                                                    toSort.Sort(new Comparer());
                                                                    toSort.ToArray();
                                                                }) + "ms");

    }

   private List<SimpleObject> GenerateCollection()
   {
       int length = 10000000;
       var list = new List<SimpleObject>();
       for (int i=0; i<length; i++)
       {
           list.Add(new SimpleObject());
       }
       return list;
   }

    private long Measure(Action method)
    {
        Stopwatch sw = new Stopwatch();
        sw.Reset();
        sw.Start();
        method();
        sw.Stop();
        return sw.ElapsedMilliseconds;
    }

    public class Comparer: IComparer<SimpleObject>
    {
        public int Compare(SimpleObject x, SimpleObject y)
        {
            return x.A.CompareTo(y.A);
        }
    }
    public class SimpleObject: IComparable<SimpleObject>
    {
    public int A;
    private static Random rand = new Random();
    private const int maxValue = 1000000;

    public SimpleObject()
    {
        A = rand.Next(maxValue);
    }

    public int CompareTo(SimpleObject other)
    {
        return A.CompareTo(other.A);
    }
}

Here are results:

OrderBy x.A: 8732ms
OrderBy x: 19136ms
OrderBy x using Comparer: 17054ms
OrderBy x using Comparer<int>.Default: 8758ms
OrderBy x using Comparer<SimpleObject>.Default: 19817ms
Sort: 8000ms
Sort using Comparison: 9515ms
Sort using Comparer: 8990ms

I wonder why OrderBy using Comparer works longer then without Comparer and why Sort using Comparison works longer the using Comparer.

PSsam
  • 639
  • 7
  • 18
  • [try giving a look at this](http://stackoverflow.com/a/1832706/1376657). – Nadir Sampaoli Jun 22 '12 at 08:41
  • It explains the difference between OrderBy and Sort, but doesn't explain why using custom Comparer takes a lot of time. – PSsam Jun 22 '12 at 08:47
  • The difference between Sort overloads is not really significant, but that between OrderBy overloads is of more interest. If you don't supply a comparer, the following comparer is used: `(IComparer) Comparer.Default`. How do things stack up if you specify this comparer explicitly? – spender Jun 22 '12 at 08:47
  • have you tried running it through a profiler? IMHO the only way to really say what is happening here (aside from using some form of decompiler) – Random Dev Jun 22 '12 at 08:54

1 Answers1

0

It seems the problem in comparison of objects. An OrderBy method caches keys. int keys are compared quickly and object keys are compared long.

PSsam
  • 639
  • 7
  • 18