2012-06-22 1 views
4

정렬 메서드의 코드 테스트 속도를 작성했습니다. 콜렉션을 생성하고 다른 메소드를 사용하여 콜렉션을 정렬합니다.왜 Enumerable.OrderBy <TSource, TKey>가 Comparer을 사용하지 않을 때 메서드가 더 빨리 작동합니까?

OrderBy x.A: 8732ms 
OrderBy x: 19136ms 
OrderBy x using Comparer: 17054ms 
OrderBy x using Comparer<int>.Default: 8758ms 
OrderBy x using Comparer<SimpleObject>.Default: 19817ms 
Sort: 8000ms 
Sort using Comparison: 9515ms 
Sort using Comparer: 8990ms 

내가있는 OrderBy 사용하여 비교 자 정렬 비교가 더 이상 사용하여 비교자를 작동하여 비교 자 및 이유없이 다음 이상 작동 왜 궁금 : 여기

public void TestMethod1() 
    { 
     var unsortedCollection = GenerateCollection(); 

     var toSort = unsortedCollection.ToList(); 
     Console.WriteLine("OrderBy x.A: " + Measure(() => toSort.OrderBy(x => x.A).ToArray()) + "ms"); 
     toSort = unsortedCollection.ToList(); 
     Console.WriteLine("OrderBy x: " + Measure(() => toSort.OrderBy(x => x).ToArray()) + "ms"); 
     toSort = unsortedCollection.ToList(); 
     Console.WriteLine("OrderBy x using Comparer: " + Measure(() => toSort.OrderBy(x => x, new Comparer()).ToArray()) + "ms"); 
     toSort = unsortedCollection.ToList(); 
     Console.WriteLine("OrderBy x using Comparer<int>.Default: " + Measure(() => toSort.OrderBy(x => x.A, Comparer<int>.Default).ToArray()) + "ms"); 
     toSort = unsortedCollection.ToList(); 
     Console.WriteLine("OrderBy x using Comparer<SimpleObject>.Default: " + Measure(() => toSort.OrderBy(x => x, Comparer<SimpleObject>.Default).ToArray()) + "ms"); 

     toSort = unsortedCollection.ToList(); 
     Console.WriteLine("Sort: " + Measure(() => 
               { 
                toSort.Sort(); 
                toSort.ToArray(); 
               }) + "ms"); 
     toSort = unsortedCollection.ToList(); 
     Console.WriteLine("Sort using Comparison: " + Measure(() => 
                    { 
                     toSort.Sort((x, y) => x.A.CompareTo(y.A)); 
                     toSort.ToArray(); 
                    }) + "ms"); 
     toSort = unsortedCollection.ToList(); 
     Console.WriteLine("Sort using Comparer: " + Measure(() => 
                   { 
                    toSort.Sort(new Comparer()); 
                    toSort.ToArray(); 
                   }) + "ms"); 

    } 

    private List<SimpleObject> GenerateCollection() 
    { 
     int length = 10000000; 
     var list = new List<SimpleObject>(); 
     for (int i=0; i<length; i++) 
     { 
      list.Add(new SimpleObject()); 
     } 
     return list; 
    } 

    private long Measure(Action method) 
    { 
     Stopwatch sw = new Stopwatch(); 
     sw.Reset(); 
     sw.Start(); 
     method(); 
     sw.Stop(); 
     return sw.ElapsedMilliseconds; 
    } 

    public class Comparer: IComparer<SimpleObject> 
    { 
     public int Compare(SimpleObject x, SimpleObject y) 
     { 
      return x.A.CompareTo(y.A); 
     } 
    } 
    public class SimpleObject: IComparable<SimpleObject> 
    { 
    public int A; 
    private static Random rand = new Random(); 
    private const int maxValue = 1000000; 

    public SimpleObject() 
    { 
     A = rand.Next(maxValue); 
    } 

    public int CompareTo(SimpleObject other) 
    { 
     return A.CompareTo(other.A); 
    } 
} 

결과입니다.

+0

[이것 좀보세요] (http://stackoverflow.com/a/1832706/1376657). –

+0

OrderBy와 Sort의 차이를 설명하지만 사용자 정의 Comparer을 사용하는 데 많은 시간이 걸리는 이유는 설명하지 않습니다. – PSsam

+0

정렬 오버로드의 차이는 실제로 중요하지 않지만 OrderBy 오버로드 간의 차이는 더 중요합니다. 비교자를 제공하지 않으면 다음 비교자를 사용합니다.'(IComparer ) Comparer .Default'. 이 비교자를 명시 적으로 지정하면 작업은 어떻게 스택 업됩니까? – spender

답변

0

개체를 비교할 때 문제가되는 것 같습니다. OrderBy 메서드는 키를 캐시합니다. int 키는 빠르게 비교되고 객체 키는 오래 비교됩니다.

관련 문제