Skip to content

Instantly share code, notes, and snippets.

@aevitas
Last active November 16, 2016 13:06
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 aevitas/68cfd7e7f926e0ef4a90f01934247c9a to your computer and use it in GitHub Desktop.
Save aevitas/68cfd7e7f926e0ef4a90f01934247c9a to your computer and use it in GitHub Desktop.
private static void BenchmarkReadsMarshal()
{
byte[] buffer = new byte[Marshal.SizeOf(typeof(int))];
fixed (byte* b = buffer)
for (int i = 0; i < numIterations; i++)
Marshal.PtrToStructure<int>(new IntPtr(b));
}
private static void BenchmarkReadsUnsafe()
{
int x = 100;
int size = Unsafe.SizeOf<int>();
for (int i = 0; i < numIterations; i++)
Unsafe.Read<int>(&x);
}
private static void BenchmarkWritesMarshal()
{
int num = 100;
int size = Marshal.SizeOf<int>();
byte[] buffer = new byte[size];
fixed (byte* b = buffer)
for (int i = 0; i < numIterations; i++)
Marshal.StructureToPtr(num, new IntPtr(b), false);
}
private static void BenchmarkWritesUnsafe()
{
int dest = 0;
int num = 100;
for (int i = 0; i < numIterations; i++)
Unsafe.Write(&dest, num);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment