Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace UnsafePerformance
- {
- class Program
- {
- static void Main(string[] args)
- {
- int bufferSize = 8000;
- int numberOfIterations = 10000;
- byte[] buf = new byte[bufferSize];
- for (int i2 = 0; i2 < bufferSize; i2++) buf[i2] = (byte)(i2 % 256);
- Stopwatch sw = new Stopwatch();
- int i = 0;
- int cnt = 0;
- while (true)
- {
- cnt = 0;
- sw.Restart();
- for (i = 0; i < numberOfIterations; i++)
- for (int j = 0; j < buf.Length; j++) cnt += buf[j];
- sw.Stop();
- Console.WriteLine(sw.Elapsed);
- unsafe
- {
- cnt = 0;
- sw.Restart();
- for (i = 0; i < numberOfIterations; i++)
- for (int j = 0; j < buf.Length; j++) cnt += buf[j];
- sw.Stop();
- Console.WriteLine(sw.Elapsed);
- fixed (byte* ptr = buf)
- {
- cnt = 0;
- sw.Restart();
- for (i = 0; i < numberOfIterations; i++)
- for (byte* j = ptr, lim = ptr + bufferSize; j < lim; j++) cnt += *j;
- sw.Stop();
- Console.WriteLine(sw.Elapsed);
- }
- }
- Console.ReadLine();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement