Performance comparison of array sorting methods (3): Analysis of LINQ sort implementation

Source: Internet
Author: User
Tags bool comparison sort

The last time we analyzed the implementation of the Array.sort<t> method and learned that the class library would use high-performance sorting methods for some special cases--int arrays are such an example, so the performance of the test results is particularly high. However, from the data, even under normal circumstances,,array.sort<t> performance is higher than the LINQ sort. But also a friend from the test to draw the conclusion is the opposite, this is why? So now, let's analyze how the LINQ sort is implemented, and hopefully this will give you a sense of the difference in performance.

Unfortunately, LINQ-sorted code in the System.Core.dll assembly, Microsoft did not release this part of the source code, we have to use the. NET reflector to find out.

Definition, use, and extension of LINQ sort interfaces

The so-called LINQ ordering is the use of several extension methods defined in the System.Linq.Enumerable class, which are:

public static IOrderedEnumerable<TSource> OrderBy<TSource, TKey> (
   this IEnumerable<TSource> source, Func<TSource, TKey>  keySelector);

public static IOrderedEnumerable<TSource> OrderBy<TSource, TKey>(
   this IEnumerable<TSource> source, Func<TSource, TKey> keySelector,  IComparer<TKey> comparer);

public static IOrderedEnumerable<TSource> OrderByDescending<TSource,  TKey>(
   this IEnumerable<TSource> source, Func<TSource, TKey>  keySelector);

public static IOrderedEnumerable<TSource> OrderByDescending<TSource,  TKey>(
   this IEnumerable<TSource> source, Func<TSource, TKey> keySelector,  IComparer<TKey> comparer);

For ease of use, I tend to add additional interfaces, such as:

public static IOrderedEnumerable<TSource> OrderBy<TSource, TKey> (
   this IEnumerable<TSource> source, Func<TSource, TKey>  keySelector, bool decending)
{
   return decending ?
     source.OrderByDescending(keySelector) :
     source.OrderBy(keySelector);
}

When used, you can use a Boolean value to indicate the direction of the sort (ascending or descending) without having to "manually" select one from the two methods. In addition, it's a bit of a hassle to construct a icomparer<tkey> type, so I went on to extend an interface that uses a delegate object as a "comparer", as Array.sort<t> has done:

public static IOrderedEnumerable<TSource> OrderBy<TSource, TKey> (
   this IEnumerable<TSource> source, Func<TSource, TKey>  keySelector, Comparison<TKey> compare, bool decending)
{
   return decending ?
     source.OrderByDescending(keySelector, new FunctorComparer<TKey>(compare))  :
     source.OrderBy(keySelector, new FunctorComparer<TKey>(compare));
}

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.