Last active
November 16, 2016 13:06
-
-
Save aevitas/68cfd7e7f926e0ef4a90f01934247c9a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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