【发布时间】:2013-10-15 09:36:25
【问题描述】:
我试图实现一个 Enumerable 来复制 LINQ OrderBy 所做的事情。为此,我使用了两种方法:
- 我使用 Mono 的 Linq/Enumerable.cs 作为基础,并在我的 Enumerable 中复制了
OrderBy代码。 - 我使用反编译器来了解 .NET 的版本并复制了该代码。
在对 LINQ 版本与两个选项进行基准测试时(我使用 .Take(10) 打印更少的输出),linq 版本明显更快(1900 毫秒对 3700 毫秒)。源数据为List<MyObject>,按 char 成员排序。优化build flag was on。
谁能解释一下这种差异可能来自哪里?
编辑: 我将在下面概述 2. 的代码:
这里,Buffer<TElement> 是 System.Linq.Buffer<TElement> 的副本(从 ILSpy 复制,因为它是内部的),Sort (大部分)以相同的方式从 System.Linq.EnumerableSorter<TElement> 复制。
public class Query2F
{
private Func<Lineitem, char> keySelector;
private char[] keys;
private IComparer<char> comparer;
private bool descending;
public Query2F(Func<Lineitem, char> keySelector, bool descending)
{
this.keySelector = keySelector;
this.descending = descending;
this.comparer = Comparer<char>.Default;
}
public static IEnumerable<Lineitem> Execute(List<Lineitem> lineitem)
{
Buffer<Lineitem> buffer = new Buffer<Lineitem>(lineitem);
if (buffer.count > 0)
{
Query2F q2f = new Query2F((s => s.returnflag), false);
int[] array = q2f.Sort(buffer.items, buffer.count);
q2f = null;
for (int i = 0; i < buffer.count; i++)
{
yield return buffer.items[array[i]];
}
}
yield break;
}
private void ComputeKeys(Lineitem[] elements, int count)
{
this.keys = new char[count];
for (int i = 0; i < count; i++)
{
//this.keys[i] = this.keySelector(elements[i]);
this.keys[i] = elements[i].returnflag;
}
}
private int CompareKeys(int index1, int index2)
{
int num = this.comparer.Compare(this.keys[index1], this.keys[index2]);
if (num == 0)
{
return index1 - index2;
}
else
{
if (!this.descending)
{
return num;
}
return -num;
}
}
internal int[] Sort(Lineitem[] elements, int count)
{
this.ComputeKeys(elements, count);
int[] array = new int[count];
for (int i = 0; i < count; i++)
{
array[i] = i;
}
this.QuickSort(array, 0, count - 1);
return array;
}
private void QuickSort(int[] map, int left, int right)
{
do
{
int num = left;
int num2 = right;
int index = map[num + (num2 - num >> 1)];
do
{
if (num < map.Length)
{
if (this.CompareKeys(index, map[num]) > 0)
{
num++;
continue;
}
}
while (num2 >= 0 && this.CompareKeys(index, map[num2]) < 0)
{
num2--;
}
if (num > num2)
{
break;
}
if (num < num2)
{
int num3 = map[num];
map[num] = map[num2];
map[num2] = num3;
}
num++;
num2--;
}
while (num <= num2);
if (num2 - left <= right - num)
{
if (left < num2)
{
this.QuickSort(map, left, num2);
}
left = num;
}
else
{
if (num < right)
{
this.QuickSort(map, num, right);
}
right = num2;
}
}
while (left < right);
}
}
internal struct Buffer<TElement>
{
internal TElement[] items;
internal int count;
internal Buffer(IEnumerable<TElement> source)
{
TElement[] array = null;
int num = 0;
ICollection<TElement> collection = source as ICollection<TElement>;
if (collection != null)
{
num = collection.Count;
if (num > 0)
{
array = new TElement[num];
collection.CopyTo(array, 0);
}
}
else
{
foreach (TElement current in source)
{
if (array == null)
{
array = new TElement[4];
}
else
{
if (array.Length == num)
{
TElement[] array2 = new TElement[checked(num * 2)];
Array.Copy(array, 0, array2, 0, num);
array = array2;
}
}
array[num] = current;
num++;
}
}
this.items = array;
this.count = num;
}
internal TElement[] ToArray()
{
if (this.count == 0)
{
return new TElement[0];
}
if (this.items.Length == this.count)
{
return this.items;
}
TElement[] array = new TElement[this.count];
Array.Copy(this.items, 0, array, 0, this.count);
return array;
}
}
【问题讨论】:
-
看起来这取决于您所做的实际实现?
-
实际实现是相当多的代码,因为它使用 System.LINQ 内部的类和方法,我还必须复制它们。我将尝试在上面概述它。
-
当你使用第三方
LINQ的东西时,有可能提供者没有以最好的方式实现它,所以你的问题可以理解,你只能知道原因你知道第三部分LINQ东西的内部实现。 -
现在你失去了我。 第三方是指反编译 Microsoft LINQ代码?我的假设是,使用此代码,我的
Enumerable和.Net实现应该大致相同。这就是我不理解性能差异的原因。除非反编译后的代码和原代码有区别或者.NET对系统库使用了更好的优化。 -
您的“等效”代码是否在“发布”模式(与“调试”模式相对)下编译?仅此一项就可能产生重大影响。
标签: c# performance linq mono ienumerable