Skip to content

Instantly share code, notes, and snippets.

@ermau
Created October 20, 2011 20:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ermau/1302220 to your computer and use it in GitHub Desktop.
Save ermau/1302220 to your computer and use it in GitHub Desktop.
/*
Times for 10,000 iterations averaged over 300 tests in milliseconds
Test 2 bytes 4 bytes 6 bytes 8 bytes
Array 0.739094 0.771695 0.774858 0.759280
Buffer 0.644463 0.634443 0.646658 0.650585
Unsafe value 0.504277 0.615360 0.742236 0.851658
Hybrid UnsafeValue 0.609504 0.476190 0.558644 0.236927
Hybrid BlockCopy 0.608247 0.767905 0.883720 0.251436
Hybrid PtrSafe 0.584011 0.487495 0.678595 0.249857
Hybrid PtrUnsafe 0.562200 0.477424 0.621765 0.237684
*/
using System;
using System.Linq;
using System.Threading;
using Cadenza;
namespace TestCopies
{
class Program
{
static unsafe void Main(string[] args)
{
Action<byte[], long> bufferCopy = (b,v) =>
{
byte[] vbytes = BitConverter.GetBytes (v);
Buffer.BlockCopy (vbytes, 0, b, 0, b.Length);
};
Action<byte[], long> arrayCopy = (b,v) =>
{
byte[] vbytes = BitConverter.GetBytes (v);
Array.Copy (vbytes, 0, b, 0, b.Length);
};
Action<byte[], long> unsafeValueCopy = (b, v) =>
{
long[] avalue = new []{ v };
fixed (long* vptr = avalue)
fixed (byte* bptr = b)
{
byte* vbuff = (byte*)vptr;
for (int i = 0; i < b.Length; ++i)
*(bptr + i) = *(vbuff + i);
}
};
Action<byte[], long> hybridUnsafeCopy = (b, v) =>
{
fixed (byte* bptr = b)
{
if (b.Length >= sizeof(int) && b.Length < sizeof(long))
{
(*(int*)bptr) = (int)(v & ~(v >> (sizeof(int) * 8)));
if (v > Int32.MaxValue)
{
long[] avalue = new[] { v };
fixed (long* vptr = avalue)
{
byte* vbuff = (byte*)vptr;
for (int i = sizeof (int); i < b.Length; ++i)
*(bptr + i) = *(vbuff + i);
}
}
}
else if (b.Length >= sizeof(long))
{
(*(long*)bptr) = v;
}
else
{
long[] avalue = new [] { v };
fixed (long* vptr = avalue)
{
byte* vbuff = (byte*)vptr;
for (int i = 0; i < b.Length; ++i)
*(bptr + i) = *(vbuff + i);
}
}
}
};
Action<byte[], long> hybridSetPtrSafeCopy = (b, v) =>
{
fixed (byte* bptr = b)
{
if (b.Length >= sizeof(int) && b.Length < sizeof(long))
{
(*(int*)bptr) = (int)(v & ~(v >> (sizeof(int) * 8)));
if (v > Int32.MaxValue)
{
byte[] vb = BitConverter.GetBytes (v);
for (int i = sizeof (int); i < b.Length; ++i)
*(bptr + i) = vb[i];
}
}
else if (b.Length >= sizeof(long))
{
(*(long*)bptr) = v;
}
else
{
long[] avalue = new [] { v };
fixed (long* vptr = avalue)
{
byte* vbuff = (byte*)vptr;
for (int i = 0; i < b.Length; ++i)
*(bptr + i) = *(vbuff + i);
}
}
}
};
Action<byte[], long> hybridSetPtrUnsafeCopy = (b, v) =>
{
fixed (byte* bptr = b)
{
if (b.Length >= sizeof(int) && b.Length < sizeof(long))
{
(*(int*)bptr) = (int)(v & ~(v >> (sizeof(int) * 8)));
if (v > Int32.MaxValue)
{
fixed (byte* vb = BitConverter.GetBytes (v))
{
for (int i = sizeof (int); i < b.Length; ++i)
*(bptr + i) = *(vb + i);
}
}
}
else if (b.Length >= sizeof(long))
{
(*(long*)bptr) = v;
}
else
{
long[] avalue = new [] { v };
fixed (long* vptr = avalue)
{
byte* vbuff = (byte*)vptr;
for (int i = 0; i < b.Length; ++i)
*(bptr + i) = *(vbuff + i);
}
}
}
};
Action<byte[], long> hybridBlockCopyCopy = (b, v) =>
{
fixed (byte* bptr = b)
{
if (b.Length >= sizeof(int) && b.Length < sizeof(long))
{
(*(int*)bptr) = (int)(v & ~(v >> (sizeof(int) * 8)));
if (v > Int32.MaxValue)
{
byte[] vb = BitConverter.GetBytes (v);
Buffer.BlockCopy (vb, sizeof (int), b, sizeof (int), b.Length - sizeof (int));
}
}
else if (b.Length >= sizeof(long))
{
(*(long*)bptr) = v;
}
else
{
long[] avalue = new [] { v };
fixed (long* vptr = avalue)
{
byte* vbuff = (byte*)vptr;
for (int i = 0; i < b.Length; ++i)
*(bptr + i) = *(vbuff + i);
}
}
}
};
long value = 1000000000000;
while (true)
{
Console.WriteLine ("{0,-20}{1,-10}{2,-10}{3,-10}{4,-10}", "Test", "2 bytes", "4 bytes", "6 bytes", "8 bytes");
Console.WriteLine ("{0,-20}{1,-10:N6}{2,-10:N6}{3,-10:N6}{4,-10:N6}",
"Array",
arrayCopy.Timings (new byte[2], value, 300, 10000).Average (t => t.TotalMilliseconds),
arrayCopy.Timings (new byte[4], value, 300, 10000).Average (t => t.TotalMilliseconds),
arrayCopy.Timings (new byte[6], value, 300, 10000).Average (t => t.TotalMilliseconds),
arrayCopy.Timings (new byte[8], value, 300, 10000).Average (t => t.TotalMilliseconds));
Console.WriteLine ("{0,-20}{1,-10:N6}{2,-10:N6}{3,-10:N6}{4,-10:N6}",
"Buffer",
bufferCopy.Timings (new byte[2], value, 300, 10000).Average (t => t.TotalMilliseconds),
bufferCopy.Timings (new byte[4], value, 300, 10000).Average (t => t.TotalMilliseconds),
bufferCopy.Timings (new byte[6], value, 300, 10000).Average (t => t.TotalMilliseconds),
bufferCopy.Timings (new byte[8], value, 300, 10000).Average (t => t.TotalMilliseconds));
Console.WriteLine ("{0,-20}{1,-10:N6}{2,-10:N6}{3,-10:N6}{4,-10:N6}",
"Unsafe value",
unsafeValueCopy.Timings (new byte[2], value, 300, 10000).Average (t => t.TotalMilliseconds),
unsafeValueCopy.Timings (new byte[4], value, 300, 10000).Average (t => t.TotalMilliseconds),
unsafeValueCopy.Timings (new byte[6], value, 300, 10000).Average (t => t.TotalMilliseconds),
unsafeValueCopy.Timings (new byte[8], value, 300, 10000).Average (t => t.TotalMilliseconds));
Console.WriteLine ("{0,-20}{1,-10:N6}{2,-10:N6}{3,-10:N6}{4,-10:N6}",
"Hybrid UnsafeValue",
hybridUnsafeCopy.Timings (new byte[2], value, 300, 10000).Average (t => t.TotalMilliseconds),
hybridUnsafeCopy.Timings (new byte[4], value, 300, 10000).Average (t => t.TotalMilliseconds),
hybridUnsafeCopy.Timings (new byte[6], value, 300, 10000).Average (t => t.TotalMilliseconds),
hybridUnsafeCopy.Timings (new byte[8], value, 300, 10000).Average (t => t.TotalMilliseconds));
Console.WriteLine ("{0,-20}{1,-10:N6}{2,-10:N6}{3,-10:N6}{4,-10:N6}",
"Hybrid BlockCopy",
hybridBlockCopyCopy.Timings (new byte[2], value, 300, 10000).Average (t => t.TotalMilliseconds),
hybridBlockCopyCopy.Timings (new byte[4], value, 300, 10000).Average (t => t.TotalMilliseconds),
hybridBlockCopyCopy.Timings (new byte[6], value, 300, 10000).Average (t => t.TotalMilliseconds),
hybridBlockCopyCopy.Timings (new byte[8], value, 300, 10000).Average (t => t.TotalMilliseconds));
Console.WriteLine ("{0,-20}{1,-10:N6}{2,-10:N6}{3,-10:N6}{4,-10:N6}",
"Hybrid PtrSafe",
hybridSetPtrSafeCopy.Timings (new byte[2], value, 300, 10000).Average (t => t.TotalMilliseconds),
hybridSetPtrSafeCopy.Timings (new byte[4], value, 300, 10000).Average (t => t.TotalMilliseconds),
hybridSetPtrSafeCopy.Timings (new byte[6], value, 300, 10000).Average (t => t.TotalMilliseconds),
hybridSetPtrSafeCopy.Timings (new byte[8], value, 300, 10000).Average (t => t.TotalMilliseconds));
Console.WriteLine ("{0,-20}{1,-10:N6}{2,-10:N6}{3,-10:N6}{4,-10:N6}",
"Hybrid PtrUnsafe",
hybridSetPtrUnsafeCopy.Timings (new byte[2], value, 300, 10000).Average (t => t.TotalMilliseconds),
hybridSetPtrUnsafeCopy.Timings (new byte[4], value, 300, 10000).Average (t => t.TotalMilliseconds),
hybridSetPtrUnsafeCopy.Timings (new byte[6], value, 300, 10000).Average (t => t.TotalMilliseconds),
hybridSetPtrUnsafeCopy.Timings (new byte[8], value, 300, 10000).Average (t => t.TotalMilliseconds));
Console.WriteLine();
Thread.Sleep (5000);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment