1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| unsafe class Program
{
[DllImport("msvcrt.dll", EntryPoint = "memset", CallingConvention = CallingConvention.Cdecl, SetLastError = false)]
public static extern IntPtr MemSet(IntPtr dest, int c, IntPtr count);
private static byte* thePtr;
static void Main(string[] args)
{
int size = 1 << 30;
byte[] array = new byte[size];
unsafe
{
fixed (byte* ptr = array)
{
thePtr = ptr;
Action[] actions = new Action[4];
for (int i = 0; i < actions.Length; i++)
{
int start = i* (array.Length / actions.Length);
int count = array.Length / actions.Length;
actions[i] = () => CleanArray(start, count);
}
Stopwatch sw = new Stopwatch();
sw.Start();
//CleanArray(0, array.Length);
Parallel.Invoke(actions);
var result = sw.ElapsedMilliseconds;
Console.WriteLine((result));
Console.ReadLine();
}
}
}
private static unsafe void CleanArray(int start, int count)
{
MemSet((IntPtr)(thePtr + start), 0, (IntPtr)count);
} |
Partager