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?

msc
  • 33,420
  • 29
  • 119
  • 214
Sato
  • 8,192
  • 17
  • 60
  • 115
  • well, you could bench that. But if `doSomething` is a big procedure, it will make no visible difference – Jean-François Fabre Nov 13 '17 at 06:08
  • 1
    @Jean-FrançoisFabre - With no optimizations, GCC already produces the exact same code even in a small procedure. And with O2, it considers the two code pieces identical (so much it calls on one to do the others job). https://godbolt.org/g/LxVDGx – StoryTeller - Unslander Monica Nov 13 '17 at 06:13
  • 1
    It depends on the way compiler will translate both and the way computer will optimize at runtime, so it is impossible to answer very firmly. Anyway, it is very unlikely to observe any major difference. – Jean-Baptiste Yunès Nov 13 '17 at 06:14
  • @StoryTeller I misread, I thought variable was global in one case. ok – Jean-François Fabre Nov 13 '17 at 06:15
  • 1
    [Should I declare a variable inside or outside the main function?](https://stackoverflow.com/q/36482383/995714), [Declaring variables inside loops, good practice or bad practice?](https://stackoverflow.com/q/7959573/995714), [Declaring variables inside or outside of a loop](https://stackoverflow.com/q/8803674/995714) – phuclv Nov 13 '17 at 06:56

6 Answers6

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.

msc
  • 33,420
  • 29
  • 119
  • 214
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

Ahmed Rajab
  • 603
  • 3
  • 10
  • 28
0

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.

Raymond Hettinger
  • 216,523
  • 63
  • 388
  • 485
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).

Bonje Fir
  • 787
  • 8
  • 25
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

sorush-r
  • 10,490
  • 17
  • 89
  • 173
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();
}
Pang
  • 9,564
  • 146
  • 81
  • 122