int:
BitConverter.ToInt32(new byte[] { w, x, y, z }, 0);BitConverter.ToUInt32(new byte[] { w, x, y, z }, 0);b = new byte[] { w, x, y, z };
BitConverter.ToInt32(b, 0);b = new byte[] { 1, 2, 3, 4, 5, 6, 7, w, x, y, z };
BitConverter.ToInt32(b, 7);w | (x << 8) | (y << 16) | (z << 24);b[0] | (b[1] << 8) | (b[2] << 16) | (b[3] << 24);
10 ^ 9 Release (x86), 2,5 Core i7 . ( , , BitConverter, ):
test1: 00:00:15.5287282 67305985
test2: 00:00:15.1334457 67305985
test3: 00:00:08.0648586 67305985
test4: 00:00:11.2307059 67305985
test5: 00:00:02.0219417 67305985
test6: 00:00:01.6275684 67305985
, :
- test1 , , 15 , , , . ( 60 ?)
- test2 ,
uint int . , , , , . - test3 , (7ns) , , , .
- test4 ,
ToInt32 (3ns) - test5 , 4 ,
ToInt32. - test6 , 4 , ! , - .
, test6, , ( ). , 1 . !
:
using System;
namespace BitConverterTest
{
class Program
{
const int iters = 1000000000;
static void Main(string[] args)
{
test1(1, 2, 3, 4);
test2(1, 2, 3, 4);
test3(1, 2, 3, 4);
test4(1, 2, 3, 4);
test5(1, 2, 3, 4);
test6(1, 2, 3, 4);
}
static void test1(byte w, byte x, byte y, byte z)
{
int res = 0;
var timer = System.Diagnostics.Stopwatch.StartNew();
for (int i = 0; i < iters; i++)
res = BitConverter.ToInt32(new byte[] { w, x, y, z }, 0);
Console.WriteLine("test1: " + timer.Elapsed + " " + res);
}
static void test2(byte w, byte x, byte y, byte z)
{
uint res = 0;
var timer = System.Diagnostics.Stopwatch.StartNew();
for (int i = 0; i < iters; i++)
res = BitConverter.ToUInt32(new byte[] { w, x, y, z }, 0);
Console.WriteLine("test2: " + timer.Elapsed + " " + res);
}
static void test3(byte w, byte x, byte y, byte z)
{
int res = 0;
var timer = System.Diagnostics.Stopwatch.StartNew();
var b = new byte[] { w, x, y, z };
for (int i = 0; i < iters; i++)
res = BitConverter.ToInt32(b, 0);
Console.WriteLine("test3: " + timer.Elapsed + " " + res);
}
static void test4(byte w, byte x, byte y, byte z)
{
int res = 0;
var timer = System.Diagnostics.Stopwatch.StartNew();
var b = new byte[] { 1, 2, 3, 4, 5, 6, 7, w, x, y, z };
for (int i = 0; i < iters; i++)
res = BitConverter.ToInt32(b, 7);
Console.WriteLine("test4: " + timer.Elapsed + " " + res);
}
static void test5(byte w, byte x, byte y, byte z)
{
int res = 0;
var timer = System.Diagnostics.Stopwatch.StartNew();
var b = new byte[] { w, x, y, z };
for (int i = 0; i < iters; i++)
res = w | (x << 8) | (y << 16) | (z << 24);
Console.WriteLine("test5: " + timer.Elapsed + " " + res);
}
static void test6(byte w, byte x, byte y, byte z)
{
int res = 0;
var timer = System.Diagnostics.Stopwatch.StartNew();
var b = new byte[] { w, x, y, z };
for (int i = 0; i < iters; i++)
res = b[0] | (b[1] << 8) | (b[2] << 16) | (b[3] << 24);
Console.WriteLine("test6: " + timer.Elapsed + " " + res);
}
}
}