2011-11-12 11 views
5

Sto analizzando un codice C#. Il metodo di seguito è uno dei più costosi. Ai fini di questa domanda, supponiamo che la micro-ottimizzazione sia la cosa giusta da fare. Esiste un approccio per migliorare le prestazioni di questo metodo?Ottimizza il frammento di codice C#

Modificare il parametro di input su p su ulong[] creerebbe una inefficienza della macro.

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; 
    } 
} 
+3

Sembra BitConverter.ToInt64 - http://msdn.microsoft.com/en-us/library/system.bitconverter.toint64.aspx? –

+0

Leggi e sposta alcuni byte - è onestamente costoso? Sono sicuro che lo chiamerai molto, ma sarei sorpreso se il compilatore potesse andare molto male con quello – Rup

+1

@Alexei ToUInt64, ma si. Se volevi usarlo, invece, postalo come risposta? (Oppure Eric vuole ottimizzare anche BitConverter?) – Rup

risposta

5

Perché non utilizzare BitConverter? Devo credere che Microsoft abbia dedicato un po 'di tempo a sintonizzare quel codice. Inoltre si occupa di problemi di endian.

Ecco come BitConverter trasforma un byte [] in un lungo/ulong (ulong converte come firmato e poi l'inserisce in unsigned):

[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)); 
    } 
} 

ho il sospetto che facendo la conversione una parola a 32 bit a un tempo è per l'efficienza a 32 bit. Niente registri a 64 bit su una CPU a 32 bit significa che gestire un inte a 64 bit è molto più costoso.

Se si è certi che si sta prendendo di mira l'hardware a 64 bit, potrebbe essere più veloce eseguire la conversione in un colpo solo.

+0

Vale anche la pena notare che questo utilizza codice non sicuro per migliorare le prestazioni; sembra che il metodo BCL sia la soluzione migliore. –

+0

D'oh! Utilizzato BitConverter in un contesto diverso oggi e non ci ha pensato. A proposito, questa ottimizzazione ha migliorato le prestazioni complessive della mia porta C# di CityHash del 30% (rendendo ora il 28% più veloce rispetto alla versione C++ da cui sono stato eseguito il porting). –

1

Per riferimento, NET 4.0 BitConverter.ToInt64 (Shared Source Initiative a http://referencesource.microsoft.com/netframework.aspx) di Microsoft:

// 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); 
       } 
      } 
     } 
    } 
1

Perché non andare pericoloso?

unsafe static ulong Fetch64(byte[] p, int ofs = 0) 
{ 
    fixed (byte* bp = p) 
    { 
    return *((ulong*)(bp + ofs)); 
    } 
}