Why stack is faster than heap and what exactly is stack?

Dear all,

This question puzzles me for a while,
Why stack is faster than heap, and what exactly is stack?

I know there are many places discussed this problem like,

But I am still confused.
The main issue is that, they say both stack and heap are in the memory (RAM).
If so, how can stack be faster than heap? After all they are all in memory, the speed is depend on the the speed of the memory like bandwidth and cas latency. I simply do not see any reason that stack will be much faster than heap, if they are both in the RAM.

I mean, if both stack and heap are in RAM, then why do we even need heap? Just put everything on the stack and that is it.

My understanding is that, in order to make stack really useful, stack should be in the CPU cache, so it is small but way faster than RAM.

Eg, my laptop is xeon 2186M with
Cache size: L1 - 384 KB, L2 - 1.5 MB, L3 - 12 MB.
The speed of L1 is 10 times L2, the speed of L2 is 10 times L3, the speed of L3 is 10 times RAM.

So when heap-arrays in intel Fortran, I should set the heap size smaller than 12MB.
So like below, I set it to 10240KB so 10MB.

In some of my code, actually I do notice that if I set heap-arrays as 0, it can be 50% slower than setting heap-arrays as 128, 256, 512, …10240 etc. I can almost certain the speedup by setting non-zero heap array size is that, some arrays are really stored in cache than in memory.

What do you guys think about stack and heap, and how to use them and set them?

Thank you very much in advance.

2 Likes