我正在尝试对一对int数组进行排序(int [] a; int [] b;)
如果我使用Array.Sort(a,b)那么表现很棒。
但是,我更喜欢使用List<>并在结构中加载int对。 我可以使用带有重载的Array.Sort()来实现这一点,该重载为结构提供了一个简单的比较器,但它比Array.Sort(a,b)慢了大约4倍
这是正常的吗?
答案 0 :(得分:9)
这听起来很现实;你引入了更多的复杂性(更多的查找等,更多的虚拟方法,更多的范围检查),所以它只会变慢(数组访问非常直接,非常快)。
看起来你可以通过实现IComparer<T>
而不是委托方法来更快地获得它(但不像数组那么快); (修改)并使用IComparable<T>
再次加快速度:
Array.Sort: 2241ms
List.Sort (delegate): 8714ms
List.Sort (interface): 6976ms
List.Sort (comparable): 5456ms
使用代码:
using System;
using System.Collections.Generic;
using System.Diagnostics;
struct MyStruct : IComparable<MyStruct>
{
private readonly int key, value;
public int Key { get { return key; } }
public int Value { get { return value; } }
public MyStruct(int key, int value)
{
this.key = key;
this.value = value;
}
public int CompareTo(MyStruct other)
{
return key.CompareTo(other.key);
}
}
static class Program
{
static void Main()
{
const int SIZE = 10000000;
int[] a = new int[SIZE], b = new int[SIZE];
Random rand = new Random();
for(int i = 0 ; i < SIZE ; i++) {
a[i] = rand.Next();
b[i] = i;
}
var list = new List<MyStruct>(SIZE);
for (int i = 0; i < SIZE; i++)
{
list.Add(new MyStruct(a[i], b[i]));
}
var list2 = new List<MyStruct>(list);
var list3 = new List<MyStruct>(list);
var watch = Stopwatch.StartNew();
Array.Sort(a, b);
watch.Stop();
Console.WriteLine("Array.Sort: " + watch.ElapsedMilliseconds + "ms");
watch = Stopwatch.StartNew();
list.Sort((x, y) => x.Key.CompareTo(y.Key));
watch.Stop();
Console.WriteLine("List.Sort (delegate): " + watch.ElapsedMilliseconds + "ms");
watch = Stopwatch.StartNew();
list2.Sort(MyComparer.Default);
watch.Stop();
Console.WriteLine("List.Sort (interface): " + watch.ElapsedMilliseconds + "ms");
watch = Stopwatch.StartNew();
list3.Sort();
watch.Stop();
Console.WriteLine("List.Sort (comparable): " + watch.ElapsedMilliseconds + "ms");
}
sealed class MyComparer : IComparer<MyStruct>
{
private MyComparer() { }
public static readonly MyComparer Default = new MyComparer();
public int Compare(MyStruct x, MyStruct y)
{
return x.Key.CompareTo(y.Key);
}
}
}