Bri*_*ian 4 c# arrays performance ienumerable
所以,我有一个内部有数组的类.目前,我对类的项目进行枚举的策略是使用代码foreach (item x in classInstance.InsideArray).我更愿意使用foreach (item x in classInstance)并使数组私有化.我主要担心的是我真的需要避免任何缓慢的事情; 阵列受到很多打击(并且有几百个项目).枚举这个数组是很便宜的.一种想法只是拥有类实现IEnumerable<item>,但InsideArray.getEnumerator()只给我一个非泛型的枚举器.我也尝试过实现IEnumerable界面.这工作但很慢,可能是由于拳击.
有没有办法让这个类本身可以在没有性能损失的情况下进行枚举?
正常代码:
//Class
public class Foo {
//Stuff
public Item[,] InsideArray {get; private set;}
}
//Iteration. Shows up all over the place
foreach (Item x in classInstance.InsideArray)
{
//doStuff
}
Run Code Online (Sandbox Code Playgroud)
调整后,代码慢得多:
//Class
public class Foo : IEnumerable {
//Stuff
private Item[,] InsideArray;
System.Collections.IEnumerator System.Collections.IEnumerable GetEnumerator()
{
return InsideArray.GetEnumerator();
}
}
//Iteration. Shows up all over the place
foreach (Item x in classInstance)
{
//doStuff
}
Run Code Online (Sandbox Code Playgroud)
注意:为非泛型迭代器添加实现是可能的,并且比我的慢速解决方案更快,但它仍然比直接使用数组更糟糕.我希望有办法以某种方式告诉C#,"嘿,当我要求你迭代这个对象迭代它的数组,同样快,"但显然这是不可能的......至少从建议的答案迄今.
定制的迭代器可能会更快(编辑为以已知类型返回):
Basic: 2468ms - -2049509440
Bespoke: 1087ms - -2049509440
Run Code Online (Sandbox Code Playgroud)
(您将直接使用ArrayIterator作为Foo的GetEnumerator - 实质上是从ArrayEnumerator.GetEnumerator复制代码;我的观点是显示类型迭代器比接口更快)
使用代码:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
class Foo
{
public struct ArrayIterator<T> : IEnumerator<T>
{
private int x, y;
private readonly int width, height;
private T[,] data;
public ArrayIterator(T[,] data)
{
this.data = data;
this.width = data.GetLength(0);
this.height = data.GetLength(1);
x = y = 0;
}
public void Dispose() { data = null; }
public bool MoveNext()
{
if (++x >= width)
{
x = 0;
y++;
}
return y < height;
}
public void Reset() { x = y = 0; }
public T Current { get { return data[x, y]; } }
object IEnumerator.Current { get { return data[x, y]; } }
}
public sealed class ArrayEnumerator<T> : IEnumerable<T>
{
private readonly T[,] arr;
public ArrayEnumerator(T[,] arr) { this.arr = arr; }
public ArrayIterator<T> GetEnumerator()
{
return new ArrayIterator<T>(arr);
}
System.Collections.Generic.IEnumerator<T> System.Collections.Generic.IEnumerable<T>.GetEnumerator()
{
return GetEnumerator();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
public int[,] data;
public IEnumerable<int> Basic()
{
foreach (int i in data) yield return i;
}
public ArrayEnumerator<int> Bespoke()
{
return new ArrayEnumerator<int>(data);
}
public Foo()
{
data = new int[500, 500];
for (int x = 0; x < 500; x++)
for (int y = 0; y < 500; y++)
{
data[x, y] = x + y;
}
}
static void Main()
{
Test(1); // for JIT
Test(500); // for real
Console.ReadKey(); // pause
}
static void Test(int count)
{
Foo foo = new Foo();
int chk;
Stopwatch watch = Stopwatch.StartNew();
chk = 0;
for (int i = 0; i < count; i++)
{
foreach (int j in foo.Basic())
{
chk += j;
}
}
watch.Stop();
Console.WriteLine("Basic: " + watch.ElapsedMilliseconds + "ms - " + chk);
watch = Stopwatch.StartNew();
chk = 0;
for (int i = 0; i < count; i++)
{
foreach (int j in foo.Bespoke())
{
chk += j;
}
}
watch.Stop();
Console.WriteLine("Bespoke: " + watch.ElapsedMilliseconds + "ms - " + chk);
}
}
Run Code Online (Sandbox Code Playgroud)