Upon comparing test code with Sam, I determined that we are both right!
However, about different things:
- Accessing memory (reading and writing) is just as fast wherever it is - stack, global or heap.
- Allocating it, however, is fastest on stack and slowest on heap.
It goes like this: stack
< global
< heap
. (allocation time)
Technically, stack allocation isn't really an allocation, the runtime just makes sure a part of the stack (frame?) is reserved for the array.
I strongly advise being careful with this, though.
I recommend the following:
- When you need to create arrays frequently which never leave the function (e.g. by passing its reference), using the stack will be an enormous improvement.
- If you can recycle an array, do so whenever you can! The heap is the best place for long-term object storage. (polluting global memory isn't nice; stack frames can disappear)
(Note: 1. only applies to value types; reference types will be allocated on the heap and the benefit will be reduced to 0)
To answer the question itself: I have not encountered any problem at all with any large-stack test.
I believe the only possible problems are a stack overflow, if you are not careful with your function calls and running out of memory when creating your thread(s) if the system is running low.
The section below is my initial answer. It is wrong-ish and the tests aren't correct. It is kept only for reference.
My test indicates the stack-allocated memory and global memory is at least 15% slower than (takes 120% the time of) heap-allocated memory for usage in arrays!
This is my test code, and this is a sample output:
Stack-allocated array time: 00:00:00.2224429
Globally-allocated array time: 00:00:00.2206767
Heap-allocated array time: 00:00:00.1842670
------------------------------------------
Fastest: Heap.
| S | G | H |
--+---------+---------+---------+
S | - | 100.80 %| 120.72 %|
--+---------+---------+---------+
G | 99.21 %| - | 119.76 %|
--+---------+---------+---------+
H | 82.84 %| 83.50 %| - |
--+---------+---------+---------+
Rates are calculated by dividing the row's value to the column's.
I tested on Windows 8.1 Pro (with Update 1), using an i7 4700 MQ, under .NET 4.5.1
I tested both with x86 and x64 and the results are identical.
Edit: I increased the stack size of all threads 201 MB, the sample size to 50 million and decreased iterations to 5.
The results are the same as above:
Stack-allocated array time: 00:00:00.4504903
Globally-allocated array time: 00:00:00.4020328
Heap-allocated array time: 00:00:00.3439016
------------------------------------------
Fastest: Heap.
| S | G | H |
--+---------+---------+---------+
S | - | 112.05 %| 130.99 %|
--+---------+---------+---------+
G | 89.24 %| - | 116.90 %|
--+---------+---------+---------+
H | 76.34 %| 85.54 %| - |
--+---------+---------+---------+
Rates are calculated by dividing the row's value to the column's.
Though, it seems the stack is actually getting slower.
Marshal.AllocHGlobal
(don't forget toFreeHGlobal
too) to allocate the data outside of managed memory? Then cast the pointer to afloat*
, and you should be sorted. – Marc Gravell♦