Lets consider following two codes
First:
for (int i=0;i<10000000;i++)
{
char* tab = new char[500];
delete[] tab;
}
Second:
for (int i=0;i<10000000;i++)
{
char tab[500];
}
The peak memory usage is almost the same, but the second code runs about 20 times faster than the first one.
Question
Is it because in first code array is stored on heap, and in the second one array is stored on stack?
Yes, Stack allocation is much faster as all the second code sample is doing is moving (adding/subtracting) the stack pointer rather than manipulating the heap.
If you want to know more, these two questions cover the subject