Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8215139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T11:41:14+00:00 2026-06-07T11:41:14+00:00

Results Using a list of 10 million random int s (same seed each time,

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T11:41:15+00:00Added an answer on June 7, 2026 at 11:41 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How to fill up the AutoCompleteCustomSource List with the query results using Data reader
I want to display my arraylist results by using custom list view. for now
I have tried fetching MySQL query results using mysql_fetch_row() and mysql_result() and numeric values
Have a lot of unnecessary results using contains() method in my query. Don't tell
Is there any conceivable reason why I would see different results using unicode string
What do I use in the value to return results using the following CAML
I'm trying to create pagination for search results using MySQL and ColdFusion. My intention
How do you specify the the row terminator when outputting query results using sqlcmd?
like the plots in: http://www.cs.berkeley.edu/~ballard/cs267.sp11/hw1/results/ Using some statistics tool(such as R, matlab) could achieve
In my web application, I display the search results using XSLT. There are some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.