0

I know that allocating memory from the stack is faster, But when accessing a variable which is faster Eg:

void fn1()
{
    int *p=new int[50];
    for(int i=0;i<50;i++)
        p+i=i*i;
}

void fn2()
{
    int p[50];
    for(int i=0;i<50;i++)
        p[i]=i*i;
}

which of the above functions will be executed faster?

I asked it because I am creating a game engine in c++ and I have frequent draw calls which are quite small just involving transformation,rotations,etc by matrices so which allocation will most useful for my purpose

147956
  • 45
  • 5
  • 3
    Do you mean the stack or heap? – Hugo Corrá Dec 20 '13 at 11:25
  • why don't you test it? – codeling Dec 20 '13 at 11:26
  • 1
    There are no guarantees that I know of either way, especially on [NUMA](http://en.wikipedia.org/wiki/Non-uniform_memory_access) machines, but normally `new` is _very_ slow in comparison to just reserving space on the stack. Question is though, what will you be using the memory for after filling it with data? If you're going to return the data, heap allocated data won't need copying, while stack data will. – Joachim Isaksson Dec 20 '13 at 11:26
  • possible duplicate of [comparison of access performance of data in heap and stack](http://stackoverflow.com/questions/3455987/comparison-of-access-performance-of-data-in-heap-and-stack) – Jesse Good Dec 20 '13 at 11:27
  • Measure The Time Maybe ;)? – codeling Dec 20 '13 at 11:28
  • also, possible duplicate of http://stackoverflow.com/questions/161053/c-which-is-faster-stack-allocation-or-heap-allocation – Hugo Corrá Dec 20 '13 at 11:29
  • It must be stack as it doesn't have the overhead of de-allocating (collapsing holes to reduce fragmentation) – Rohit Vipin Mathews Dec 20 '13 at 11:30
  • @user3122301 Depending on the resolution of your clock you may have to increase the size of the array to get a measurable time. If this question was not closed I would post how to do that using the high resolution clock from c++11. – drescherjm Dec 20 '13 at 13:18
  • [Here's](https://stackoverflow.com/a/55990305) a similar benchmark that just reads from and writes to each element of a big array. On my machine, it runs at least 5x faster when the array is on the stack. – Gumby The Green May 05 '19 at 09:06

4 Answers4

6

Although the standard does not say anything about it, on computers with uniform memory structure there should be no difference. You may see some differences in access time due to the effects of caching in the CPU, because the data from the array p allocated in the stack would be closer (in terms of address) to the data of other local variables of your function (unless the optimizer chooses to place these locals in registers) but overall access time should be the same.

The biggest hit that you are going to take with memory allocated dynamically is the calls of malloc and free.

Sergey Kalinichenko
  • 675,664
  • 71
  • 998
  • 1,399
1

It's been a while since I've delved into C...indexing into an array should be a fast operation. As a guess I would say they're going to be the same. In first function you are incrementing the pointer...in the second one you are indexing into the array which is pretty much the same as incrementing the pointer to arrive at the memory location of the appropriate array element.

chosenbreed37
  • 954
  • 7
  • 10
0

You'll take a hit allocating the array via the call to new, but after that they'll both have identical performance characteristics.

Sean
  • 55,981
  • 11
  • 86
  • 129
0

If you consider the allocation,

The 2nd function executes faster than the first. when you allocate memory on stack, the instruction are embedded such that it will move the stack pointer and almost it will be a single instruction on most of the architectures.