c# - 位运算性能,如何提高

标签 c# performance bitwise-operators bitwise-and

我有一个简单的任务:确定将一些数字(字节数组长度)编码为字节数组并编码最终值需要多少字节(实现这篇文章:Encoded Length and Value Bytes)。

最初我写了一个快速完成任务的方法:

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 class 中的示例页面来衡量性能。性能测试让我感到惊讶。我的测试方法运行了 1000 次该方法来编码一个具有 100 000 个元素的字节数组(实际上,只有数组 sixe),平均时间为:

  • 编码——大约 200 毫秒
  • 编码 2 -- 大约 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 毫秒。看来列表(不是按位运算)给我带来了显着的性能损失。

最佳答案

这里的主要问题几乎肯定是 List 的分配,以及插入新元素时以及最后将列表转换为数组时所需的分配。这段代码可能大部分时间都花在垃圾收集器和内存分配器上。相比之下,使用与不使用按位运算符可能意义不大,我会研究减少您首先分配的内存量的方法。

一种方法是发送对预先分配的字节数组的引用和数组中您所在位置的索引,而不是分配和返回数据,然后返回一个整数,告诉您已写入了多少字节。处理大数组通常比处理许多小对象更有效。正如其他人所提到的,使用探查器,看看您的代码在哪里花费时间。

当然,我提到的优化将使您的代码本质上更底层,更接近您通常在 C 中执行的操作,但通常需要在可读性和性能之间进行权衡。

关于c# - 位运算性能,如何提高,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28792529/

相关文章:

c# - 在运行时更改 DataGridView 标题文本

c# - Razor:方法 'Write' 没有重载需要 0 个参数

performance - 如何在分布式环境中提高 Lucene 性能?

c# - ConfigurationManager.AppSettings[key] 始终为空

每个函数的python时间测量

java - ArrayList add() 的行为方式

Python bitand (&) 与

javascript - ruby 与 javascript 中的按位或

c - 用条件循环每个数字的最快方法

c# - LINQ - 比较列表字典以查找新的和更改的对象