stackalloc 更快的情况:
private static volatile int _dummy; // just to avoid any optimisations
// that have us measuring the wrong
// thing. Especially since the difference
// is more noticable in a release build
// (also more noticable on a multi-core
// machine than single- or dual-core).
static void Main(string[] args)
{
System.Diagnostics.Stopwatch sw1 = new System.Diagnostics.Stopwatch();
Thread[] threads = new Thread[20];
sw1.Start();
for(int t = 0; t != 20; ++t)
{
threads[t] = new Thread(DoSA);
threads[t].Start();
}
for(int t = 0; t != 20; ++t)
threads[t].Join();
Console.WriteLine(sw1.ElapsedTicks);
System.Diagnostics.Stopwatch sw2 = new System.Diagnostics.Stopwatch();
threads = new Thread[20];
sw2.Start();
for(int t = 0; t != 20; ++t)
{
threads[t] = new Thread(DoHA);
threads[t].Start();
}
for(int t = 0; t != 20; ++t)
threads[t].Join();
Console.WriteLine(sw2.ElapsedTicks);
Console.Read();
}
private static void DoSA()
{
Random rnd = new Random(1);
for(int i = 0; i != 100000; ++i)
StackAllocation(rnd);
}
static unsafe void StackAllocation(Random rnd)
{
int size = rnd.Next(1024, 131072);
int* p = stackalloc int[size];
_dummy = *(p + rnd.Next(0, size));
}
private static void DoHA()
{
Random rnd = new Random(1);
for(int i = 0; i != 100000; ++i)
HeapAllocation(rnd);
}
static void HeapAllocation(Random rnd)
{
int size = rnd.Next(1024, 131072);
int[] a = new int[size];
_dummy = a[rnd.Next(0, size)];
}
此代码与问题中的代码之间的重要区别:
我们有几个线程在运行。通过堆栈分配,它们在自己的堆栈中进行分配。使用堆分配,它们是从与其他线程共享的堆中分配的。
分配了更大的尺寸。
每次分配不同的大小(尽管我播种了随机生成器以使测试更具确定性)。这使得堆碎片更有可能发生,使得堆分配的效率低于每次相同的分配。
除此之外,还值得注意的是,它stackalloc
通常用作将fixed
数组固定在堆上的替代方法。固定数组不利于堆性能(不仅对于该代码,而且对于使用相同堆的其他线程),因此如果声明的内存将在任何合理的时间内使用,那么性能影响会更大。
虽然我的代码演示了一个stackalloc
可以带来性能优势的案例,但在这个问题中,它可能更接近于大多数情况下有人可能会急切地“优化”使用它。希望这两段代码一起显示整体stackalloc
可以提高性能,但也会大大降低性能。
通常,stackalloc
除非您无论如何都需要使用固定内存与非托管代码进行交互,否则您甚至不应该考虑,并且应该将其视为fixed
一般堆分配的替代方案而不是替代方案。在这种情况下使用仍然需要谨慎,在开始之前进行深思熟虑,并在完成之后进行分析。
在其他情况下使用可能会带来好处,但它应该远远低于您将尝试的性能改进列表。
编辑:
回答问题的第 1 部分。Stackalloc 在概念上与您描述的差不多。它获取堆栈内存的一块,然后返回一个指向该块的指针。它不会检查内存是否适合这样,而是如果它尝试将内存获取到堆栈的末尾(在创建线程时受 .NET 保护),那么这将导致操作系统向运行时返回异常,然后它变成一个 .NET 托管异常。如果您只是在具有无限递归的方法中分配单个字节,则会发生同样的情况 - 除非调用经过优化以避免堆栈分配(有时可能),否则单个字节最终将加起来足以触发堆栈溢出异常。