1

for example

int i, a, c, d; // these vars will only be used in while
while(bigNumber--) {
    i = doSomething();
    // **

}

and

while(bigNumber--) {
   int i, a, b, c; // i'd like to put it here for readability
   i = doSomething();
   // **
}

Does it make big difference in terms of performance?

5

6 Answers 6

5

Yes. scope of variable i is different in both cases.

In first case, A variable i declared in a block or function. So, you can access it in the block or function.

In the second case, A variable I declared in a while loop. So, you can access it in while loop only.

Does it make big difference in terms of performance?

No, it will not matter performance-wise where you declare it.

For example 1:

int main()
{
    int i, bigNumber;

    while(bigNumber--) {
        i = 0;
    }
}

Assembly:

main:
  push rbp
  mov rbp, rsp
.L3:
  mov eax, DWORD PTR [rbp-4]
  lea edx, [rax-1]
  mov DWORD PTR [rbp-4], edx
  test eax, eax
  setne al
  test al, al
  je .L2
  mov DWORD PTR [rbp-8], 0
  jmp .L3
.L2:
  mov eax, 0
  pop rbp
  ret

Example 2:

int main()
{
    int bigNumber;

    while(bigNumber--) {
        int i;
        i = 0;
    }
}

Assembly:

main:
  push rbp
  mov rbp, rsp
.L3:
  mov eax, DWORD PTR [rbp-4]
  lea edx, [rax-1]
  mov DWORD PTR [rbp-4], edx
  test eax, eax
  setne al
  test al, al
  je .L2
  mov DWORD PTR [rbp-8], 0
  jmp .L3
.L2:
  mov eax, 0
  pop rbp
  ret

Both generate the same assembly code.

0
1

Q. Does it make big difference in terms of performance?

A. There is no performance difference at all. The only difference is whether the scope of the variable is visible outside the loop.

0
0

It makes a difference...When you declare a variable outside the loop you can get its value after the loop ends but if you declare it inside the loop you can't use it after the loop because it is not defined

0

Computational complexity and I/O operations are most valuable things that you must considered as performance bottleneck in the programs. There is no performance difference between these two codes. Moreover, compilers do some optimization on codes and it is not unexpected if this two codes run as the same(i.e compiler convert second code to first automatically).

0

The only difference I can see in assembly results is the location of a movl $0, -4(%rbp) instruction. In theory there may be a performance penalty for codes that use data which is spread more widely among memory (that will make your code less cache-friendly in theory)

In practise there will be no difference. Cache is big enough, Optimization will make both codes generate the same assembly, and most probably the code will be re-ordered. Following is a simple test case with all optimizations turned off and on.

Assembly results:

Assembly results

Code: Simple test case

Turning on optimizers will generate exact same result: Optimization on

0

I think if you put it like this, it will constantly redeclare the variables, so no matter what you initialize, it will get lost after the variables redeclare themselves.

while(bigNumber--) {
   int i, a, b, c; 
   i = doSomething();
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.