5

我正在分析一些 C# 代码。下面的方法是最昂贵的方法之一。出于这个问题的目的,假设微优化是正确的做法。有没有办法提高这种方法的性能?

将输入参数更改为ptoulong[]会导致宏效率低下。

static ulong Fetch64(byte[] p, int ofs = 0)
{
    unchecked
    {
        ulong result = p[0 + ofs] + 
            ((ulong) p[1 + ofs] <<  8) + 
            ((ulong) p[2 + ofs] << 16) + 
            ((ulong) p[3 + ofs] << 24) + 
            ((ulong) p[4 + ofs] << 32) + 
            ((ulong) p[5 + ofs] << 40) + 
            ((ulong) p[6 + ofs] << 48) + 
            ((ulong) p[7 + ofs] << 56);
        return result;
    }
}
4

4 回答 4

5

为什么不使用 BitConverter?我必须相信微软已经花了一些时间调整该代码。此外,它还处理字节序问题。

以下是 BitConverter 如何将 byte[] 转换为 long/ulong(ulong 将其转换为有符号然后将其转换为无符号):

[SecuritySafeCritical]
public static unsafe long ToInt64(byte[] value, int startIndex)
{
  if (value == null)
  {
    ThrowHelper.ThrowArgumentNullException(ExceptionArgument.value);
  }
  if (((ulong) startIndex) >= value.Length)
  {
    ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.startIndex, ExceptionResource.ArgumentOutOfRange_Index);
  }
  if (startIndex > (value.Length - 8))
  {
    ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_ArrayPlusOffTooSmall);
  }
  fixed (byte* numRef = &(value[startIndex]))
  {
    if ((startIndex % 8) == 0)
    {
      return *(((long*) numRef));
    }
    if (IsLittleEndian)
    {
      int num  = ((numRef[0] | (numRef[1] << 8)) | (numRef[2] << 0x10)) | (numRef[3] << 0x18);
      int num2 = ((numRef[4] | (numRef[5] << 8)) | (numRef[6] << 0x10)) | (numRef[7] << 0x18);
      return (((long) ((ulong) num)) | (num2 << 0x20));
    }
    int num3 = (((numRef[0] << 0x18) | (numRef[1] << 0x10)) | (numRef[2] << 8)) | numRef[3];
    int num4 = (((numRef[4] << 0x18) | (numRef[5] << 0x10)) | (numRef[6] << 8)) | numRef[7];
    return (((long) ((ulong) num4)) | (num3 << 0x20));
  }
}

我怀疑一次转换一个 32 位字是为了提高 32 位效率。32 位 CPU 上没有 64 位寄存器意味着处理 64 位整数要昂贵得多。

如果您确定您的目标是 64 位硬件,那么一举进行转换可能会更快。

于 2011-11-12T02:50:03.297 回答
2

尝试使用for而不是展开循环。您也许可以节省边界检查的时间。

试试 BitConverter.ToUInt64 - http://msdn.microsoft.com/en-us/library/system.bitconverter.touint64.aspx,如果它是你要找的。

于 2011-11-12T01:45:56.133 回答
1

作为参考,Microsoft 的 .NET 4.0 BitConverter.ToInt64(Shared Source Initiative 在http://referencesource.microsoft.com/netframework.aspx):

    // Converts an array of bytes into a long.
    [System.Security.SecuritySafeCritical]  // auto-generated 
    public static unsafe long ToInt64 (byte[] value, int startIndex) {
        if( value == null)  {
            ThrowHelper.ThrowArgumentNullException(ExceptionArgument.value);
        } 

        if ((uint) startIndex >= value.Length) { 
            ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.startIndex, ExceptionResource.ArgumentOutOfRange_Index); 
        }

        if (startIndex > value.Length -8) {
            ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_ArrayPlusOffTooSmall);
        }

        fixed( byte * pbyte = &value[startIndex]) {
            if( startIndex % 8 == 0) { // data is aligned 
                return *((long *) pbyte); 
            }
            else { 
                if( IsLittleEndian) {
                    int i1 = (*pbyte) | (*(pbyte + 1) << 8)  | (*(pbyte + 2) << 16) | (*(pbyte + 3) << 24);
                    int i2  = (*(pbyte+4)) | (*(pbyte + 5) << 8)  | (*(pbyte + 6) << 16) | (*(pbyte + 7) << 24);
                    return (uint)i1 | ((long)i2 << 32); 
                }
                else { 
                    int i1 = (*pbyte << 24) | (*(pbyte + 1) << 16)  | (*(pbyte + 2) << 8) | (*(pbyte + 3)); 
                    int i2  = (*(pbyte+4) << 24) | (*(pbyte + 5) << 16)  | (*(pbyte + 6) << 8) | (*(pbyte + 7));
                    return (uint)i2 | ((long)i1 << 32); 
                }
            }
        }
    } 
于 2011-11-12T03:14:19.223 回答
1

为什么不去不安全?

unsafe static ulong Fetch64(byte[] p, int ofs = 0)
{
  fixed (byte* bp = p)
  {
    return *((ulong*)(bp + ofs));
  }
}
于 2011-11-12T03:21:03.927 回答