为什么列表<> .OrderBy LINQ比IComparable接口+列表与LT更快;>的.sort在调试模式? [英] Why is List<>.OrderBy LINQ faster than IComparable+List<>.Sort in Debug mode?
问题描述
我感兴趣的是,是否会更快,或通过实现IComparable接口和List.Sort使用LINQ我的课进行排序。我很惊讶,当LINQ代码为快。
I was interested in whether it would be faster to sort my classes using LINQ, or by implementing the IComparable interface and List.Sort. I was quite surprised when the LINQ code was faster.
做测试,我做了一个非常简单的类TestSort的不那么恰当的名字,实施IComparable的
To do the test, I made a very simple class with the not-so-apt name of TestSort, implementing IComparable.
class TestSort: IComparable<TestSort> {
private int age;
private string givenName;
public int Age {
get {
return age;
}
set {
age = value;
}
}
public string GivenName {
get {
return givenName;
}
set {
givenName = value;
}
}
public TestSort(int age, string name) {
this.age = age;
this.givenName = name;
}
public int CompareTo(TestSort other) {
return this.age.CompareTo(other.age);
}
}
然后一个简单的程序很多次排序它 - 排序是不是复制列表昂贵得多,因此,该影响可以忽略。
Then a simple program to sort it many times - the sort was much more expensive than copying the list, so the effect of that can be ignored.
class Program {
static void Main(string[] args) {
// Create the test data
string name = "Mr. Bob";
Random r = new Random();
var ts2 = new List<TestSort>();
for (int i = 0; i < 100; i++) {
ts2.Add(new TestSort(r.Next(), name));
}
DateTime start, end;
// Test List<>.Sort
start = DateTime.Now;
for (int i = 0; i < 100000; i++) {
var l = ts2.ToList();
l.Sort();
}
end = DateTime.Now;
Console.WriteLine("IComparable<T>: ");
Console.WriteLine((end - start).TotalMilliseconds);
// Test Linq OrderBy
start = DateTime.Now;
for (int i = 0; i < 100000; i++) {
var l = ts2.ToList();
l = l.OrderBy(item => item.Age).ToList();
}
end = DateTime.Now;
Console.WriteLine("\nLINQ: ");
Console.WriteLine((end - start).TotalMilliseconds);
Console.WriteLine("Finished.");
Console.ReadKey();
}
}
我很惊讶地收到以下输出:
I was quite surprised to receive the following output:
IComparable<T>:
2965.1696
LINQ:
2181.1248
有时LINQ会去低于2000年,有时IComparable的会去约3000。
Sometimes LINQ would go below 2000, and sometimes IComparable would go about 3000.
当我与正常的目录及下测试它,诠释>
的 List.Sort
为1 / 4 LINQ的速度,这保持在2000左右。
When I tested it with a normal List<Int>
the List.Sort
was 1/4 the speed of LINQ, which remained at about 2000.
那么,为什么只有LINQ约66%的正常排序我的课的速度? ?难道我做错了什么我实现了IComparable接口的
So why is LINQ only about 66% the speed of the normal sort for my class? Am I doing something wrong with my implementation of IComparable?
更新:
我只是想尝试在释放模式做,是的,结果是不同的:
Update: I just thought to try doing it in release mode, and yes, the results were different:
IComparable<T>:
1593.0911
Linq:
1958.1119
不过我还是很有兴趣知道为什么IComparable接口处于调试慢模式。
But I am still very interested to know why IComparable is slower in debug mode.
推荐答案
如果你确保一切都在开始测量前,JIT编译的,你可能会得到不同的结果(我也推荐使用秒表
类测量时间):
If you make sure everything is JITed before starting the measure, you might get different results (I also recommend using the Stopwatch
class to measure time):
var ll = ts2.ToList();
ll.Sort();
ll.OrderBy(item => item.Age).ToList();
根据我的测量(添加上述代码后),IComparable的总是快(甚至在调试)
According to my measurements (after adding the above code), IComparable is always faster (even in debug).
这篇关于为什么列表<> .OrderBy LINQ比IComparable接口+列表与LT更快;>的.sort在调试模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!