简体   繁体   English

NET 4.0中的Array.Sort()发生了什么? TrySZSort()消失了吗?

[英]What happened to Array.Sort() in .NET 4.0? Is TrySZSort() gone?

I wonder why the following snippet does not give the expected result? 我不知道为什么以下代码片段没有给出预期的结果? It sorts a not too small random array and uses 3 different methods for it. 它对不太小的随机数组进行排序,并使用3种不同的方法。 I was expecting the speed to come out as so: 我期待这样的速度:

  1. Array.Sort() - fastest by using the native TrySZSort function as I recall from .NET 2.0 Array.Sort()-正如我从.NET 2.0回忆起的那样,通过使用本机TrySZSort函数来最快
  2. Descending sort by using the custom Comparer class 使用自定义的Comparer类降序排序
  3. The lambda expression sort. lambda表达式排序。

The Code: 代码:

class DescComparer : IComparer<double> {
    // simple comparison 
    // (yes, its not exactly correct ...)
    public int Compare(double x, double y) {
        return (x > y) ? -1 : 1; 
    }
}
static void Main(string[] args) {
    Stopwatch sw = new Stopwatch(); 
    Random rand = new Random(); 
    DescComparer comparer = new DescComparer();
    double[] a = new double[1000000]; 
    for (int r = 0; r < 20; r++) {

        // init array
        for (int i = 0; i < a.Length; i++) a[i] = rand.NextDouble();  
        sw.Restart();
        Array.Sort(a);
        sw.Stop();
        Console.WriteLine("ascending took: {0} ms ", sw.ElapsedMilliseconds);

        // init array
        for (int i = 0; i < a.Length; i++) a[i] = rand.NextDouble();
        sw.Restart();
        Array.Sort<double>(a, comparer);
        sw.Stop();
        Console.WriteLine("descending took: {0} ms ", sw.ElapsedMilliseconds);

        // init array
        for (int i = 0; i < a.Length; i++) a[i] = rand.NextDouble();
        sw.Restart();
        Array.Sort<double>(a, (x,y) => -x.CompareTo(y));
        sw.Stop();
        Console.WriteLine("desc lambda took: {0} ms ", sw.ElapsedMilliseconds);

    }
    Console.Read(); 
}

But strangely, it gives the following: 但奇怪的是,它给出了以下内容:

ascending took: 514 ms
descending took: 537 ms
desc lambda took: 915 ms
ascending took: 511 ms
descending took: 492 ms
desc lambda took: 923 ms
ascending took: 511 ms
descending took: 483 ms
desc lambda took: 912 ms
ascending took: 511 ms
descending took: 485 ms
desc lambda took: 914 ms
ascending took: 518 ms
descending took: 485 ms
desc lambda took: 924 ms
... a.s.o. ...

So, the lambda really is slowest. 因此,lambda确实是最慢的。 But how comes, the plain ascending Array.Sort is not faster anymore? 但是,为什么普通的Array.Sort不再更快了? Is it, because Array.Sort(T[], Comparer) has been improved or has TrySZSort simply been removed? 是因为Array.Sort(T [],Comparer)得到了改进,还是只是删除了TrySZSort? Or did I miss something? 还是我错过了什么?

(Release build, no Debug, no Reflector available right now ;) ) Thanks! (发布版本,没有调试,现在没有Reflector可用;))谢谢!

UPDATE: According to the hint by @Reed Copsey, the lambda expression is not fair. 更新:根据@Reed Copsey的提示,lambda表达式不公平。 I tried to change it to the same as the comparer does. 我试图将其更改为与比较器相同的值。 The speed went up. 速度加快了。 Asc / lambda went from 55% -> 75%. Asc / Lambda从55%-> 75%。 So it still is considerably slower. 因此它仍然相当慢。

So, the lambda really is slowest. 因此,lambda确实是最慢的。 But how comes, the plain ascending Array.Sort is not faster anymore? 但是,为什么普通的Array.Sort不再更快了? Is it, because Array.Sort(T[], Comparer) has been improved or has TrySZSort simply been removed? 是因为Array.Sort(T [],Comparer)得到了改进,还是只是删除了TrySZSort? Or did I miss something? 还是我错过了什么?

Well, there's two issues here. 好吧,这里有两个问题。 First, this really depends on your build and system - 首先,这实际上取决于您的构建和系统-

On my system, in x64, Array.Sort() is the fastest, by a significant amount: 在我的系统上,在x64中, Array.Sort() 最快的,速度非常可观:

ascending took: 192 ms
descending took: 248 ms
desc lambda took: 326 ms
ascending took: 194 ms
descending took: 247 ms
desc lambda took: 326 ms

On x86, things are slightly different - but not nearly the same significance as you're showing: 在x86上,情况略有不同-但与显示的意义并不完全相同:

ascending took: 235 ms
descending took: 223 ms
desc lambda took: 325 ms
ascending took: 234 ms
descending took: 222 ms
desc lambda took: 325 ms

Did you have the visual studio host attached when you ran these timings? 运行这些计时时,您是否已连接Visual Studio主机? Even a release build will be dramatically slower if you run within VS (ie: F5 instead of Ctrl+F5 by default). 如果您在VS中运行,则即使是发行版版本也将大大降低运行速度(即:默认情况下为F5而不是Ctrl + F5)。


Also note that your test isn't completely fair, in regards to the lambda. 另请注意,就lambda而言,您的测试并不完全公平。 You should use the same mechanism for testing, which would be: 您应该使用相同的测试机制,这将是:

Array.Sort<double>(a, (x, y) => (x > y) ? -1 : 1);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM