Performance comparison of array sorting methods (2):array.sort<t> implementation analysis

Source: Internet
Author: User
Tags comparison reflector sort

Yesterday we compared the performance of the Array.sort<t> method with the LINQ sort, knowing that the performance of the LINQ sort lagged significantly behind the Array.sort<t> method. And for Array.sort<t>, the most performance is the use of comparer<int> Default as an overloaded method of the comparer. At the end of the previous article we made the assumption that since the sorting algorithm is already near a standard (fast sorting), there should not be such a large gap between the,array.sort<t> method and the LINQ sort from the algorithm point of view, which causes the difference in performance. It should be the problem of the concrete way of implementation.

Download. NET Framework's code

Since it's the difference between implementations, reading code is a straightforward choice. Talk about reading. NET code, we tend to use. NET Reflector to decompile the framework's assembly to C # code--not to exclude friends who like to observe IL, and to think that they are more direct and lower. But I think in most cases, IL can see the things from C #, and C # can not understand the IL can not help, so many "by Il discovery problem" article is actually only oneself and oneself in the cool.

However, although. NET Reflector compiles assemblies into very good C # code, it is still impossible to recover much of the information before it. For example, a local variable name is not known, which makes it difficult to understand the intent of the code. For example, for readability we might write conditional statements separately, while the C # compiler might be able to connect them together. As for the obvious things to be removed, such as annotations, let alone. Therefore, I am not inclined to use. NET Reflector in cases where I can read the code directly.

You might say, "It's not nonsense, but there are some class libraries--such as the. NET Framework doesn't provide the source code, so what's the solution?" In fact, Microsoft has already announced. NET Framework quite a portion of the Assembly's source code (almost all v2.0 assemblies, such as Mscrolib, System,system.web, and so on), and they can actually be downloaded directly to local use Netmassdownloader. The suite code publishes a PDB file for easy debugging, but this is another topic, and we're only concerned with the source code.

Therefore, I recommend that you download all the source code from Microsoft to the local area. If you don't understand the reverse compilation of. NET Reflector, consider the source code-or the source code that contains the annotation.

Array.sort<t> Method implementation

The overloads of each array.sort<t> method are eventually delegated to the following overload to execute:

public static void Sort<T>(T[] array, int index, int length,  IComparer<T> comparer)
{
   ...

   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);
   }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.