Speed up BitConverter

    General discussion

  • I had to use BitConverter for decoding WAV files but it was somewhat slow.

    short oldway = System.BitConverter.ToInt16(a, 0); 
    short newway = (short) (a[0] | (a[1] << 8)); 

    On the second line, using bit operators, you get an instruction 60% faster than the original one, but attention,
    this only converts 16-bit Little-Endian !

    Note that you still have to cast the expression to short as it outputs an int.

    You can improve it further, I did find it by using Reflector to view the .Net source code.


    Tuesday, February 10, 2009 3:15 PM