ffMathy

C# unsafe code benchmark

Mar 15th, 2012
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.38 KB | None | 0 0
  1. using System;
  2.  
  3. namespace Benchmark
  4. {
  5.     class Program
  6.     {
  7.  
  8.         static void Main(string[] args)
  9.         {
  10.  
  11.             int timePassed = 0;
  12.             int timeStamp = 0;
  13.  
  14.             CalculateSafe();
  15.             CalculateUnsafe();
  16.  
  17.             timeStamp = Environment.TickCount;
  18.             CalculateSafe();
  19.             timePassed = Environment.TickCount - timeStamp;
  20.             Console.WriteLine("Safe done: " + timePassed);
  21.  
  22.             timeStamp = Environment.TickCount;
  23.             CalculateUnsafe();
  24.             timePassed = Environment.TickCount - timeStamp;
  25.             Console.WriteLine("Unsafe done: " + timePassed);
  26.  
  27.             Console.ReadLine();
  28.  
  29.         }
  30.  
  31.         static void CalculateSafe()
  32.         {
  33.  
  34.             //simple udregninger
  35.             int i = 0;
  36.             while (i < 200000000)
  37.             {
  38.                 i++;
  39.                 double o = i * 1.5;
  40.                 double l = o / 2.25;
  41.                 o--;
  42.                 l++;
  43.             }
  44.  
  45.             i = 0;
  46.             int[] array = new int[100000000];
  47.             int safe = 5;
  48.             while (i++ < array.Length - safe)
  49.             {
  50.                 int offset = i;
  51.                 int result = array[offset];
  52.                 offset++;
  53.                 result = array[offset];
  54.                 offset++;
  55.                 result = array[offset];
  56.                 offset++;
  57.                 result = array[offset];
  58.             }
  59.  
  60.         }
  61.  
  62.         //dette er unsafe (C++) kode.
  63.         static unsafe void CalculateUnsafe()
  64.         {
  65.  
  66.             //simple udregninger
  67.             int i = 0;
  68.             while (i < 200000000)
  69.             {
  70.                 i++;
  71.                 double o = i * 1.5;
  72.                 double l = o / 2.25;
  73.                 o--;
  74.                 l++;
  75.             }
  76.  
  77.             i = 0;
  78.             int[] array = new int[100000000];
  79.             int safe = 5;
  80.             fixed (int* value = &array[i])
  81.             {
  82.                 while (i++ < array.Length - safe)
  83.                 {
  84.                     int* offset = value;
  85.                     int result = *offset;
  86.                     offset++;
  87.                     result = *offset;
  88.                     offset++;
  89.                     result = *offset;
  90.                     offset++;
  91.                     result = *offset;
  92.                 }
  93.             }
  94.  
  95.         }
  96.  
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment