Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace Benchmark
- {
- class Program
- {
- static void Main(string[] args)
- {
- int timePassed = 0;
- int timeStamp = 0;
- CalculateSafe();
- CalculateUnsafe();
- timeStamp = Environment.TickCount;
- CalculateSafe();
- timePassed = Environment.TickCount - timeStamp;
- Console.WriteLine("Safe done: " + timePassed);
- timeStamp = Environment.TickCount;
- CalculateUnsafe();
- timePassed = Environment.TickCount - timeStamp;
- Console.WriteLine("Unsafe done: " + timePassed);
- Console.ReadLine();
- }
- static void CalculateSafe()
- {
- //simple udregninger
- int i = 0;
- while (i < 200000000)
- {
- i++;
- double o = i * 1.5;
- double l = o / 2.25;
- o--;
- l++;
- }
- i = 0;
- int[] array = new int[100000000];
- int safe = 5;
- while (i++ < array.Length - safe)
- {
- int offset = i;
- int result = array[offset];
- offset++;
- result = array[offset];
- offset++;
- result = array[offset];
- offset++;
- result = array[offset];
- }
- }
- //dette er unsafe (C++) kode.
- static unsafe void CalculateUnsafe()
- {
- //simple udregninger
- int i = 0;
- while (i < 200000000)
- {
- i++;
- double o = i * 1.5;
- double l = o / 2.25;
- o--;
- l++;
- }
- i = 0;
- int[] array = new int[100000000];
- int safe = 5;
- fixed (int* value = &array[i])
- {
- while (i++ < array.Length - safe)
- {
- int* offset = value;
- int result = *offset;
- offset++;
- result = *offset;
- offset++;
- result = *offset;
- offset++;
- result = *offset;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment