优化求和2个字节



我正在通过一系列字节进行迭代,并在for loop中添加另一个字节的值。

        var random = new Random();
        byte[] bytes = new byte[20_000_000]; 
        byte[] bytes2 = new byte[20_000_000];
        for (int i = 0; i < bytes.Length; i++)
        {
            bytes[i] = (byte)random.Next(255);
        }
        for (int i = 0; i < bytes.Length; i++)
        {
            bytes2[i] = (byte)random.Next(255);
        }
        //how to optimize the part below
        for (int i = 0; i < bytes.Length; i++)
        {
            bytes[i] += bytes2[i];
        }

是否有任何方法可以加快该过程,因此它可以比线性更快。

您可以使用Vector

static void Add(Span<byte> dst, ReadOnlySpan<byte> src)
{
    Span<Vector<byte>> dstVec = MemoryMarshal.Cast<byte, Vector<byte>>(dst);
    ReadOnlySpan<Vector<byte>> srcVec = MemoryMarshal.Cast<byte, Vector<byte>>(src);
    for (int i = 0; i < dstVec.Length; ++i)
    {
        dstVec[i] += srcVec[i];
    }
    for (int i = dstVec.Length * Vector<byte>.Count; i < dst.Length; ++i)
    {
        dst[i] += src[i];
    }
}

如果您在这里使用指针来对齐您的一个数组之一,则会更快。

将数组长度放在8的下一个最高倍数上。(在您的示例中已经是示例。(

使用不安全的上下文创建两个ulong数组,指向现有字节数组的开始。使用for循环迭代bytes.Length / 8次添加8个字节。

在我的系统上,它的运行速度不到13毫秒。与原始代码的105毫秒相比。

您必须添加/unsafe选项才能使用此代码。打开项目属性,然后选择"允许不安全的代码"。

var random = new Random();
byte[] bytes = new byte[20_000_000]; 
byte[] bytes2 = new byte[20_000_000];


int Len = bytes.Length >> 3; // >>3 is the same as / 8
ulong MASK =    0x8080808080808080;
ulong MASKINV = 0x7f7f7f7f7f7f7f7f;
//Sanity check
if((bytes.Length & 7) != 0) throw new Exception("bytes.Length is not a multiple of 8");
if((bytes2.Length & 7) != 0) throw new Exception("bytes2.Length is not a multiple of 8");
unsafe
{
    //Add 8 bytes at a time, taking into account overflow between bytes
   fixed (byte* pbBytes = &bytes[0])
   fixed (byte* pbBytes2 = &bytes2[0])
   {
      ulong* pBytes = (ulong*)pbBytes;
      ulong* pBytes2 = (ulong*)pbBytes2;
      for (int i = 0; i < Len; i++)
      {
        pBytes[i] = ((pBytes2[i] & MASKINV) + (pBytes[i] & MASKINV)) ^ ((pBytes[i] ^ pBytes2[i]) & MASK);
      } 
   }
}

您可以使用所有处理器/内核,假设您的计算机有多个。

Parallel.ForEach(Partitioner.Create(0, bytes.Length), range =>
{
    for (int i = range.Item1; i < range.Item2; i++)
    {
        bytes[i] += bytes2[i];
    }
});

更新: Vector<T>类也可以在.NET Framework中使用。它需要包装系统。通过向多个数据(SIMD(发出单个指令,它在单个核心中提供了并行化的优势。大多数当前的处理器均受SIMD启用。它仅针对64位进程启用,因此必须不选中标志[首选32位]。在32位进程中,属性Vector.IsHardwareAccelerated返回false,并且性能不好。

using System.Numerics;
/// <summary>Adds each pair of elements in two arrays, and replaces the
/// left array element with the result.</summary>
public static void Add_UsingVector(byte[] left, byte[] right, int start, int length)
{
    int i = start;
    int step = Vector<byte>.Count; // the step is 16
    int end = start + length - step + 1;
    for (; i < end; i += step)
    {
        // Vectorize 16 bytes from each array
        var vector1 = new Vector<byte>(left, i);
        var vector2 = new Vector<byte>(right, i);
        vector1 += vector2; // Vector arithmetic is unchecked only
        vector1.CopyTo(left, i);
    }
    for (; i < start + length; i++) // Process the last few elements
    {
        unchecked { left[i] += right[i]; }
    }
}

这比简单的循环快4-5倍,而无需使用多个线程(4核PC中的25%CPU消耗(。

最新更新