Advertisement
Jong

Heap VS Stack

Dec 11th, 2011
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.82 KB | None | 0 0
  1.     class Program
  2.     {
  3.         static void Main(string[] args)
  4.         {
  5.             System.Diagnostics.Stopwatch sw1 = new System.Diagnostics.Stopwatch();
  6.             sw1.Start();
  7.             StackAllocation();
  8.             Console.WriteLine(sw1.ElapsedTicks);
  9.  
  10.             System.Diagnostics.Stopwatch sw2 = new System.Diagnostics.Stopwatch();
  11.             sw2.Start();
  12.             HeapAllocation();
  13.             Console.WriteLine(sw2.ElapsedTicks);
  14.         }
  15.         static unsafe void StackAllocation()
  16.         {
  17.             for (int i = 0; i < 100; i++)
  18.             {
  19.                 int* p = stackalloc int[100];
  20.             }
  21.         }
  22.         static void HeapAllocation()
  23.         {
  24.             for (int i = 0; i < 100; i++)
  25.             {
  26.                 int[] a = new int[100];
  27.             }
  28.         }
  29.     }
  30.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement