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?

Kobi Burnley
  • 105
  • 1
  • 2
  • 10

2 Answers2

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.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • 1
    Clang produces the exact same assembly output for both under `-O3`. – Kevin May 01 '14 at 17:34
  • Is there any meaning to the fact that with the for loop we have a calculation (it – Kobi Burnley May 01 '14 at 17:35
  • 1
    @KobiBurnley Without optimization, yes. The compiler checks the value every loop – Kevin May 01 '14 at 17:37
  • 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. – Cloud May 01 '14 at 18:11
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.

Community
  • 1
  • 1
Floris
  • 45,857
  • 6
  • 70
  • 122
  • 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 May 01 '14 at 17:57
  • @Brendan - yes that's what I am talking about. More extensively discussed in the links I included. – Floris May 01 '14 at 17:58