1

Code looks like this:

for (int i = 0; i <= LARGE_NUMBER; ++i) {
    int x[LARGE_NUMBER] = {0};
    // do something with x
}

I think array x will be created each time when for-loop wades thru 0~LARGE_NUMBER, so this will compromise the performance? Will -O2 do some help?

10
  • 1
    Can you define it outside? It should not be too heavy (relative to object array or heap based) for stack based int arrays, but still.. Commented Feb 25, 2013 at 7:19
  • 2
    What exactly are you doing that needs both the size in the loop and in the array size? Commented Feb 25, 2013 at 7:20
  • 3
    Maybe - a profiler would tell you for sure though. How large is large though? Large arrays and the stack, generally don't mix well. Commented Feb 25, 2013 at 7:20
  • @JasonD, by LARGE_NUMBER I mean it's around 1M Commented Feb 25, 2013 at 7:31
  • Then you definitely don't want that on the stack... Commented Feb 25, 2013 at 7:31

3 Answers 3

2

Your array will be zeroed in each iteration, so definitely it will.

This code is linear time, every element of the array will be zero-initialized:

int x[LARGE_NUMBER] = {0};

And this is constant time, just increment of the stack pointer:

int x[LARGE_NUMBER];

Performance will depend on whether LARGE_NUMBER is really large or not. If LARGE_NUMBER is of size of one or two cache lines than you won't notice difference between first and second version. But if LARGE_NUMBER is really large - you will. But if your array is so large than performance difference is noticeable - than you definitely need to move it to heap. Stack space is expensive and allocation of megabytes of data in it is wrong.

If your array is really large, you can allocate it on the heap once and call memset between iteration.

Sign up to request clarification or add additional context in comments.

4 Comments

So, if I don't get x zero-initialized, then the performance wouldn't be compromised, will it?
It depends on array size. Definitely second version will be faster, but difference will be impossible to measure if array is small. In some cases you can't use second version, because your code depends on this initialization.
The performance would be compromised, since the compiler tries to allocate the memory for the array.
Compiler "allocate" memory in the stack. Stack allocation is super-fast for POD types. It's just a stack-pointer increment.
2

How is LARGE_NUMBER expected to be?

Consider that an on-stack allocation of a wide object can result wider than the stack space the system can give to a thread, and you are probably facing an "out of memory" problem even before performance can start. (The stack need to be fast, and hence in no more than few Megabytes: Ideally it has to fit the processor cache)

If that's the case a std::vector (that leave in the stack, but manages allocation from the heap) play better.

But defining it inside, makes it to be created / destroyed upon every iteration. Now: does those creation / destruction make sense (I mean: do they take some action that make sense to be repeated every time) or your problem is just to initialize to zero on every iteration? If that's the case, I wold probably do something like:

{ //just begin a scope block
    std::vector<int> v(LARGE_NUMBER); //allocates LARGE_NUMBER int-s on heap
    for (int i = 0; i <= LARGE_NUMBER; ++i)
    {
        std::fill(v.begin(), v.end(), 0); //reset at every iteration
        // other stuff with v
    }
} //here the vector and associated memory is finally eliminated

Note that the performance of std:fill is linear just like the initialization of an array, but you avoid that way to allocate/deallocate at every cycle.

In any case, your problem has O2 complexity, by definition.

2 Comments

So it does compromise the performance if I reset the array at every iteration, doesn't it?
@Alcott: Yes. But I don't know if you need it or not. What does it happen to the "other stuff with v" if it seize the previous values in it?
2

Depends on your application... i assume that you have a fixed array size? It will use: http://www.cplusplus.com/reference/cstring/memset/

#include <stdio.h>
#include <string.h>

int* x = new int[LARGE_NUMBER];
    for (int i = 0; i <= LARGE_NUMBER; ++i) {
        memset(x,0,LARGE_NUMBER);
        // do something with x
    }
    //some more stuff that needs x 
    delete[] x;

BTW: I have no C/C++ at hand to test the code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.