我有一个简单的任务:确定将一些数字(字节数组长度)编码为字节数组所需的字节数,并编码最终值(实现本文:编码长度和值字节)。
最初,我编写了一个快速完成任务的方法:
这是一段旧的代码,写得非常糟糕。
现在我正试图通过使用位运算符或者
所有的方法都按预期工作。我使用了来自Stopwatch类页面的示例来测量性能。性能测试让我感到惊讶。我的测试方法对一个具有100,000个元素的字节数组(实际上只有数组大小)进行了1,000次运行,并且平均时间如下:
问题是:你会建议如何改进
非常感谢您的帮助。
更新:感谢所有参与本主题讨论的人。我考虑了所有的建议,最终得出了这个解决方案:
最初,我编写了一个快速完成任务的方法:
public static Byte[] Encode(Byte[] rawData, Byte enclosingtag) {
if (rawData == null) {
return new Byte[] { enclosingtag, 0 };
}
List<Byte> computedRawData = new List<Byte> { enclosingtag };
// if array size is less than 128, encode length directly. No questions here
if (rawData.Length < 128) {
computedRawData.Add((Byte)rawData.Length);
} else {
// convert array size to a hex string
String hexLength = rawData.Length.ToString("x2");
// if hex string has odd length, align it to even by prepending hex string
// with '0' character
if (hexLength.Length % 2 == 1) { hexLength = "0" + hexLength; }
// take a pair of hex characters and convert each octet to a byte
Byte[] lengthBytes = Enumerable.Range(0, hexLength.Length)
.Where(x => x % 2 == 0)
.Select(x => Convert.ToByte(hexLength.Substring(x, 2), 16))
.ToArray();
// insert padding byte, set bit 7 to 1 and add byte count required
// to encode length bytes
Byte paddingByte = (Byte)(128 + lengthBytes.Length);
computedRawData.Add(paddingByte);
computedRawData.AddRange(lengthBytes);
}
computedRawData.AddRange(rawData);
return computedRawData.ToArray();
}
这是一段旧的代码,写得非常糟糕。
现在我正试图通过使用位运算符或者
BitConverter
类来优化代码。以下是一个按位运算的例子:public static Byte[] Encode2(Byte[] rawData, Byte enclosingtag) {
if (rawData == null) {
return new Byte[] { enclosingtag, 0 };
}
List<Byte> computedRawData = new List<Byte>(rawData);
if (rawData.Length < 128) {
computedRawData.Insert(0, (Byte)rawData.Length);
} else {
// temp number
Int32 num = rawData.Length;
// track byte count, this will be necessary further
Int32 counter = 1;
// simply make bitwise AND to extract byte value
// and shift right while remaining value is still more than 255
// (there are more than 8 bits)
while (num >= 256) {
counter++;
computedRawData.Insert(0, (Byte)(num & 255));
num = num >> 8;
}
// compose final array
computedRawData.InsertRange(0, new[] { (Byte)(128 + counter), (Byte)num });
}
computedRawData.Insert(0, enclosingtag);
return computedRawData.ToArray();
}
使用BitConverter
类进行最终实现:
public static Byte[] Encode3(Byte[] rawData, Byte enclosingtag) {
if (rawData == null) {
return new Byte[] { enclosingtag, 0 };
}
List<Byte> computedRawData = new List<Byte>(rawData);
if (rawData.Length < 128) {
computedRawData.Insert(0, (Byte)rawData.Length);
} else {
// convert integer to a byte array
Byte[] bytes = BitConverter.GetBytes(rawData.Length);
// start from the end of a byte array to skip unnecessary zero bytes
for (int i = bytes.Length - 1; i >= 0; i--) {
// once the byte value is non-zero, take everything starting
// from the current position up to array start.
if (bytes[i] > 0) {
// we need to reverse the array to get the proper byte order
computedRawData.InsertRange(0, bytes.Take(i + 1).Reverse());
// compose final array
computedRawData.Insert(0, (Byte)(128 + i + 1));
computedRawData.Insert(0, enclosingtag);
return computedRawData.ToArray();
}
}
}
return null;
}
所有的方法都按预期工作。我使用了来自Stopwatch类页面的示例来测量性能。性能测试让我感到惊讶。我的测试方法对一个具有100,000个元素的字节数组(实际上只有数组大小)进行了1,000次运行,并且平均时间如下:
- Encode -- 约为200毫秒
- Encode2 -- 约为270毫秒
- Encode3 -- 约为320毫秒
Encode2
方法,因为代码看起来更易读,但它的性能并不那么好。问题是:你会建议如何改进
Encode2
方法的性能或者改进Encode
的可读性?非常感谢您的帮助。
更新:感谢所有参与本主题讨论的人。我考虑了所有的建议,最终得出了这个解决方案:
public static Byte[] Encode6(Byte[] rawData, Byte enclosingtag) {
if (rawData == null) {
return new Byte[] { enclosingtag, 0 };
}
Byte[] retValue;
if (rawData.Length < 128) {
retValue = new Byte[rawData.Length + 2];
retValue[0] = enclosingtag;
retValue[1] = (Byte)rawData.Length;
} else {
Byte[] lenBytes = new Byte[3];
Int32 num = rawData.Length;
Int32 counter = 0;
while (num >= 256) {
lenBytes[counter] = (Byte)(num & 255);
num >>= 8;
counter++;
}
// 3 is: len byte and enclosing tag
retValue = new byte[rawData.Length + 3 + counter];
rawData.CopyTo(retValue, 3 + counter);
retValue[0] = enclosingtag;
retValue[1] = (Byte)(129 + counter);
retValue[2] = (Byte)num;
Int32 n = 3;
for (Int32 i = counter - 1; i >= 0; i--) {
retValue[n] = lenBytes[i];
n++;
}
}
return retValue;
}
最终,我从列表转向了固定大小的字节数组。与相同数据集的平均时间现在约为65毫秒。看来,列表(而不是位运算)使我的性能受到了显着的惩罚。