对于Split函数,在32位机器上处理ulong类型会非常慢,因此应该减少使用并改为使用uint类型。如果真的需要使用ulong,则应该实现两个版本,一个用于32位,一个用于64位。
您还应该测量一下每次处理一个字节是否更快。
需要分析内存分配的成本。如果成本足够大,请尝试在多个调用之间重复使用内存。
其他:
ToString:最好使用"(" + Offset.ToString() + ", " + Length.ToString() + ")"来提高速度。
GetHashCode: 尝试使用fixed(byte * b = & buffer[offset])
如果多次使用,此版本应该非常快。关键点:在内部数组扩展到合适的大小后不再进行新的内存分配,数据拷贝最小化。
class ArraySplitter
{
private byte[] m_data;
private int m_count;
private int[] m_stops;
private void AddRange(int start, int stop)
{
if (start > stop)
{
return;
}
if ((m_stops == null) || (m_stops.Length < (m_count + 2)))
{
int[] old = m_stops;
m_stops = new int[m_count * 3 / 2 + 4];
if (old != null)
{
old.CopyTo(m_stops, 0);
}
}
m_stops[m_count++] = start;
m_stops[m_count++] = stop;
}
public int Split(byte[] data, byte sep)
{
m_data = data;
m_count = 0;
int last = 0;
for (int i = 0; i < data.Length; i ++)
{
if (data[i] == sep)
{
AddRange(last, i - 1);
last = i + 1;
}
}
AddRange(last, data.Length - 1);
return m_count / 2;
}
public ArraySegment<byte> this[int index]
{
get
{
index *= 2;
int start = m_stops[index];
return new ArraySegment<byte>(m_data, start, m_stops[index + 1] - start + 1);
}
}
}
测试程序:
static void Main(string[] args)
{
int count = 1000 * 1000;
byte[] data = new byte[count];
for (int i = 0; i < count; i++)
{
data[i] = (byte) i;
}
Stopwatch watch = new Stopwatch();
for (int r = 0; r < 10; r++)
{
watch.Reset();
watch.Start();
int len = 0;
foreach (var seg in data.MySplit(13))
{
len += seg.Count;
}
watch.Stop();
Console.WriteLine("MySplit : {0} {1,8:N3} ms", len, watch.Elapsed.TotalMilliseconds);
watch.Reset();
watch.Start();
ArraySplitter splitter = new ArraySplitter();
int parts = splitter.Split(data, 13);
len = 0;
for (int i = 0; i < parts; i++)
{
len += splitter[i].Count;
}
watch.Stop();
Console.WriteLine("ArraySplitter: {0} {1,8:N3} ms", len, watch.Elapsed.TotalMilliseconds);
}
}
结果:
MySplit : 996093 9.514 ms
ArraySplitter: 996093 4.754 ms
MySplit : 996093 7.760 ms
ArraySplitter: 996093 2.710 ms
MySplit : 996093 8.391 ms
ArraySplitter: 996093 3.510 ms
MySplit : 996093 9.677 ms
ArraySplitter: 996093 3.468 ms
MySplit : 996093 9.685 ms
ArraySplitter: 996093 3.370 ms
MySplit : 996093 9.700 ms
ArraySplitter: 996093 3.425 ms
MySplit : 996093 9.669 ms
ArraySplitter: 996093 3.519 ms
MySplit : 996093 9.844 ms
ArraySplitter: 996093 3.416 ms
MySplit : 996093 9.721 ms
ArraySplitter: 996093 3.685 ms
MySplit : 996093 9.703 ms
ArraySplitter: 996093 3.470 ms
if
... 你难道不可能成为分支预测的受害者吗? - Kuba Wyrostek