0
int a[5] = {0}; 

VS

typedef struct 
{ 
 int a[5]; 
} ArrStruct; 
ArrStruct arrStruct; 
sizeA = sizeof(arrStruct.a)/sizeof(int);
for (it = 0 ; it < sizeA ; ++it) 
    arrStruct.a[it] = 0; 

Does initializing by for loop takes more execution time? if so, why?

2
  • 1
    int[5] is only 20 bytes, why would you even care? Commented May 1, 2014 at 17:28
  • it's for academic purposes... Commented May 1, 2014 at 17:29

2 Answers 2

2

It depends upon the compiler and the optimization flags.

On recent GCC (e.g. 4.8 or 4.9) with gcc -O3 (or probably even -O1 or -O2) it should not matter, since the same code would be emitted (GCC has even an optimization which would transform your loop into a builtin_memset which would be further optimized).

On some compilers, it could happen that the int a[5] = {0}; might be faster, because the compiler might emit e.g. vector instruction (or on x86 a rep stosw) to clear an array.

The best thing is to examine the generated (gimple representation and) assembler code (e.g. with gcc -fdump-tree-gimple -O3 -fverbose-asm -mtune=native -S) and to benchmark. Most of the cases it does not matter. Be sure to enable optimizations when compiling.

Generally, don't care about such micro-optimization; a good optimizing compiler is better than you have time to code.

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

4 Comments

Clang produces the exact same assembly output for both under -O3.
Is there any meaning to the fact that with the for loop we have a calculation (it<sizeA) every time. The question is how does the compiler put the value 0 to every element in the array.
@KobiBurnley Without optimization, yes. The compiler checks the value every loop
It is also worth noting that, depending on the architecture in use, there may be operations available to the CPU such as zeroing out or setting a large number of bytes (ie: long long words or blocks of them depending on boundary alignment) that is much, much faster than looping through elements. When in doubt, leave it to memset(), as the compiler may even further optimize it depending on what's available.
1

It depends on the scope of the variables. For a static or global variable, the first initialization

int a[5]={0};

may be done at compile time, while the loop is run at, well, run time. Thus there is no "execution" associated with the former.

You may find the discussion of this question (and in particular this answer ) interesting.

2 Comments

I assume you're referring to the fact that a smart compiler will put that global-static array in the .bss segment, which is zeroed before the program executes. But the actual zeroing isn't "at compile time". The compiler just says in the object file: "give me this much zeroed memory", and it's the OS's responsibility to actually zero it. Doesn't really affect the answer but I think it's worth thinking about out of interest.
@Brendan - yes that's what I am talking about. More extensively discussed in the links I included.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.