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
using System;

class Program
{
    static void Main()
    {
        System.Diagnostics.Stopwatch sw1 = new System.Diagnostics.Stopwatch();
        sw1.Start();
        for (int i = 0; i < 1000000; i++)
        {
            StackAllocation();
        }
        sw1.Stop();
        Console.WriteLine(sw1.ElapsedTicks); // 229729

        System.Diagnostics.Stopwatch sw2 = new System.Diagnostics.Stopwatch();
        sw2.Start();
        for (int i = 0; i < 1000000; i++)
        {
            HeapAllocation();
        }
        sw2.Stop();
        Console.WriteLine(sw2.ElapsedTicks); // 120170
    }
    static unsafe void StackAllocation()
    {
        int* p = stackalloc int[100];
    }
    static void HeapAllocation()
    {
        int[] a = new int[100];
    }
}