1

I was wondering if there will be any performance or readability difference in the following "for" declaration? Which one is preferred?

somecode.c or somecode.cpp

int i;
for(i = 0; i < 10; i++){
    // Do something
}
for(i = 25; i < 50; i++){
    // Do something else
}
for(i = 50; i >= 0; i--){
    // Do something else
}

somecode.c or somecode.cpp

for(int i = 0; i < 10; i++){
    // Do something
}
for(int i = 25; i < 50; i++){
    // Do something else
}
for(int i = 50; i >= 0; i--){
    // Do something else
}
Nkzlxs
  • 11
  • 2
  • 8
    The rule of thumb is, keep variable scopes as narrow as possible. Therefore, declaring the counter variable so it is scoped to the loop is *excellent* practice. There is no performance difference whatsoever. It is a code-correctness issue. – Cody Gray - on strike Dec 16 '20 at 06:10
  • 8
    The only reason to declare `i` **before** the loop is if you need the value of `i` **after** the loop. And typically, that's only needed when there's a `break` statement in the loop that can terminate the loop early. – user3386109 Dec 16 '20 at 06:17
  • 2
    Look at the generated assembly output. But any sane compiler should generate the same code for both versions. Concerning readability it's pretty obvious. – Jabberwocky Dec 16 '20 at 06:36
  • 3
    It is a matter of courtesy to readers of the code. In modern code, the second version is so common, that if you use the first version, it is a hint to the reader that something special is going on. So, if nothing special is going on, use the second version. – j6t Dec 16 '20 at 07:22
  • 1
    This type of question suggests a fundamental misunderstanding of how to approach optimization. – paddy Dec 16 '20 at 07:38

5 Answers5

2

The performance will be the same.

The advantage to putting i inside the for loop is that you won't accidentally use it later, since it won't be valid outside the loop. This is unimportant is a small example, but in "real" code it can be easier to understand and less error-prone.

Mark Harrison
  • 297,451
  • 125
  • 333
  • 465
1

From a style perspective: plenty of good reasons to scope as local as possible as discussed above in the comments and other answers.

From a compiled code perspective using optimization flags: none

You can compare both versions side by side on godbolt here: https://www.godbolt.org/z/T9K9Ec Notice the identical compiler output.

selbie
  • 100,020
  • 15
  • 103
  • 173
1

If you plan to use i variable later on (e.g. with the value you got in for loop), then you should use it out of the for loop scope. Otherwise, try to keep it always in the body of for statements, as your code would be much easier to read for other people and you will not make mistakes because of misuse of that variable.

whiskeyo
  • 873
  • 1
  • 9
  • 19
0

As the value of "I" is garbage once you are out of a loop. It is a good practice to free up memory as soon as a task is completed. If you initialize "I" within the loop then memory will be freed once you are out of the loop else the variable will take space as long as the function or program is running.

Sagun Devkota
  • 495
  • 3
  • 10
0

I found another reason for declaring int i before a loop.

The complier will throw error if you initialize int inside a loop before C99 standard.

error: ‘for’ loop initial declarations are only allowed in C99 mode

Nkzlxs
  • 11
  • 2