If you're doing a lot of binary file I/O requiring to store and retrieve a load of Int32 and Int64, you might want to have these snippets to speed stuff up. These are unchecked converters. After trying around for ages, these are the fastest I managed to create:
public static System.Int32 ToInt32(byte[] data, int offset)
{
int x,y,z,a;
unchecked
{
x = data[offset+1] << 8;
y = data[offset+2] << 16;
z = data[offset+3] << 24;
a = data[offset] + x + y + z;
}
return a;
}
public static System.Int64 ToInt64(byte[] data, int offset)
{
long l;
uint a;
int b;
unchecked
{
a = (uint)ToInt32(data, offset);
b = ToInt32(data, offset+4);
l = (long)a + ((long)b << 32);
}
return l;
}
The times are on a P3-933 with PC133 SDRAM:
203ms versus 1453ms for my ToInt32 versus the BitConverter.
578ms versus 1468ms for my ToInt64 versus the BitConverter.
Converters the opposite way are following once I'm done researching the fastest.
Question
Glowstick
If you're doing a lot of binary file I/O requiring to store and retrieve a load of Int32 and Int64, you might want to have these snippets to speed stuff up. These are unchecked converters. After trying around for ages, these are the fastest I managed to create:
public static System.Int32 ToInt32(byte[] data, int offset) { int x,y,z,a; unchecked { x = data[offset+1] << 8; y = data[offset+2] << 16; z = data[offset+3] << 24; a = data[offset] + x + y + z; } return a; } public static System.Int64 ToInt64(byte[] data, int offset) { long l; uint a; int b; unchecked { a = (uint)ToInt32(data, offset); b = ToInt32(data, offset+4); l = (long)a + ((long)b << 32); } return l; }The times are on a P3-933 with PC133 SDRAM:
203ms versus 1453ms for my ToInt32 versus the BitConverter.
578ms versus 1468ms for my ToInt64 versus the BitConverter.
Converters the opposite way are following once I'm done researching the fastest.
Link to comment
Share on other sites
0 answers to this question
Recommended Posts