对于 Split,在 32 位机器上处理 ulong 真的很慢,所以一定要减少到 uint。如果你真的想要ulong,实现两个版本,一个是32位的,一个是64位的。
您还应该衡量一次处理字节是否更快。
需要剖析内存分配的成本。如果它足够大,请尝试在多个调用中重用内存。
其他:
ToString:使用 "(" + Offset.ToString() + ", " + Length.ToString() + ")" 更快;
GetHashCode:尝试固定(字节 * b = & 缓冲区 [偏移量])
如果多次使用,这个版本应该非常快。关键点:内部数组扩展到合适大小后没有新的内存分配,最小的数据复制。
class ArraySplitter
{
private byte[] m_data;
private int m_count;
private int[] m_stops;
private void AddRange(int start, int stop)
{
// Skip empty range
if (start > stop)
{
return;
}
// Grow array if needed
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; // reuse m_stops
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