title: Why is List<>.OrderBy LINQ faster than IComparable+List<>.Sort in Debug mode?
body: <p>I was interested in whether it would be faster to sort my classes using LINQ, or by implementing the IComparable interface and List.Sort. I was quite surprised when the LINQ code was faster.</p>
  
  <p>To do the test, I made a very simple class with the not-so-apt name of TestSort, implementing IComparable.</p>
  
  <pre><code>class TestSort: IComparable&lt;TestSort&gt;  {
      private int age;
      private string givenName;
  
      public int Age {
          get {
              return age;
          }
          set {
              age = value;
          }
      }
  
      public string GivenName {
          get {
              return givenName;
          }
          set {
              givenName = value;
          }
      }
  
      public TestSort(int age, string name) {
          this.age = age;
          this.givenName = name;
      }
  
      public int CompareTo(TestSort other) {
          return this.age.CompareTo(other.age);
      }
  }
  </code></pre>
  
  <p>Then a simple program to sort it many times - the sort was much more expensive than copying the list, so the effect of that can be ignored.</p>
  
  <pre><code>class Program {
      static void Main(string[] args) {
          // Create the test data
          string name = "Mr. Bob";
  
          Random r = new Random();
          var ts2 = new List&lt;TestSort&gt;();
  
          for (int i = 0; i &lt; 100; i++) {
              ts2.Add(new TestSort(r.Next(), name));
          }
  
          DateTime start, end;
  
          // Test List&lt;&gt;.Sort
          start = DateTime.Now;
          for (int i = 0; i &lt; 100000; i++) {
              var l = ts2.ToList();
              l.Sort();
          }
          end = DateTime.Now;
  
          Console.WriteLine("IComparable&lt;T&gt;: ");
          Console.WriteLine((end - start).TotalMilliseconds);
  
  
          // Test Linq OrderBy
          start = DateTime.Now;
          for (int i = 0; i &lt; 100000; i++) {
              var l = ts2.ToList();
              l = l.OrderBy(item =&gt; item.Age).ToList();
          }
          end = DateTime.Now;
  
          Console.WriteLine("\nLINQ: ");
          Console.WriteLine((end - start).TotalMilliseconds);
  
          Console.WriteLine("Finished.");
          Console.ReadKey();
      }
  }
  </code></pre>
  
  <p>I was quite surprised to receive the following output:</p>
  
  <pre><code>IComparable&lt;T&gt;:
  2965.1696
  
  LINQ:
  2181.1248
  </code></pre>
  
  <p>Sometimes LINQ would go below 2000, and sometimes IComparable would go about 3000.</p>
  
  <p>When I tested it with a normal <code>List&lt;Int&gt;</code> the <code>List.Sort</code> was 1/4 the speed of LINQ, which remained at about 2000.</p>
  
  <p>So why is LINQ only about 66% the speed of the normal sort for my class? Am I doing something wrong with my implementation of IComparable?</p>
  
  <p><strong>Update:</strong>
  I just thought to try doing it in release mode, and yes, the results were different:</p>
  
  <pre><code>IComparable&lt;T&gt;:
  1593.0911
  
  Linq:
  1958.1119
  </code></pre>
  
  <p>But I am still very interested to know why IComparable is slower in debug mode.</p>