// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; using System.Runtime.InteropServices; namespace E { [StructLayout(LayoutKind.Explicit)] internal struct Cvt { [FieldOffset(0)] public int b0; [FieldOffset(1)] public int b1; [FieldOffset(2)] public int b2; [FieldOffset(3)] public int b3; [FieldOffset(4)] public int b4; [FieldOffset(5)] public int b5; [FieldOffset(6)] public int b6; [FieldOffset(7)] public int b7; [FieldOffset(0)] public long longval; } public class Program { public static long Standard(byte[] b) { return BitConverter.ToInt64(b, 0); } public static long Union(byte[] b) { Cvt tmp = new Cvt(); tmp.b0 = b[0]; tmp.b1 = b[1]; tmp.b2 = b[2]; tmp.b3 = b[3]; tmp.b4 = b[4]; tmp.b5 = b[5]; tmp.b6 = b[6]; tmp.b7 = b[7]; return tmp.longval; } private const int N = 100000000; public static void Main(string[] args) { byte[] b = { 1, 2, 3, 4, 5, 6, 7, 8 }; Console.WriteLine(Standard(b)); Console.WriteLine(Union(b)); DateTime dt1 = DateTime.Now; for(int i = 0; i < N; i++) { long v = Standard(b); } DateTime dt2 = DateTime.Now; Console.WriteLine((dt2 - dt1).TotalSeconds); DateTime dt3 = DateTime.Now; for(int i = 0; i < N; i++) { long v = Union(b); } DateTime dt4 = DateTime.Now; Console.WriteLine((dt4 - dt3).TotalSeconds); } } }