[Mono-bugs] [Bug 530450] New: Sorting a primitive List<T> is slower than sorting a primitive T []

bugzilla_noreply at novell.com bugzilla_noreply at novell.com
Wed Aug 12 12:09:46 EDT 2009


           Summary: Sorting a primitive List<T> is slower than sorting a
                    primitive T []
    Classification: Mono
           Product: Mono: Class Libraries
           Version: SVN
          Platform: Other
        OS/Version: Other
            Status: NEW
          Severity: Normal
          Priority: P5 - None
         Component: CORLIB
        AssignedTo: mono-bugs at lists.ximian.com
        ReportedBy: amcgovern at novell.com
         QAContact: mono-bugs at lists.ximian.com
          Found By: ---

In this testcase both functions should take ~ the same time. On my system
sorting a List takes 5.5 seconds and sorting the array takes 3.3 seconds.

>From what i remember from looking at this before - Array.Sort<T> fastpaths
primitive types to strongly typed methods. So Array.Sort<int> will hit a
quicksort (int[]) method. However, List<T>.Sort () calls the Array.Sort<T>
overload passing in Comparer<T>.Default and this prevents us from hitting the
quicksort (int[]) fastpath.

using System;
using System.Threading;
using System.Collections.Generic;

public class MainClass
    static void Main (string[] args)
        Random r = new Random (0);
        List<int> list = new List<int>();
        for (int i = 0; i < 10000000; i++)
            list.Add (r.Next ());
        int [] array = list.ToArray ();

        DateTime start = DateTime.Now;
        list.Sort ();
        TimeSpan sortList = DateTime.Now - start;

        start = DateTime.Now;
        Array.Sort<int> (array);
        TimeSpan sortArray = DateTime.Now - start;

        Console.WriteLine ("Sorting the list took: {0:0.00} seconds",
        Console.WriteLine ("Sorting the array took: {0:0.00} seconds",

Configure bugmail: http://bugzilla.novell.com/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the QA contact for the bug.
You are the assignee for the bug.

More information about the mono-bugs mailing list