1

Im not really sure how to formulate this question, so ill just try to picture it this way... Will this:

#include <iostream>

int main()
{
  int i;
  while (true)
    for (i = 0; i < 100; i++)
      std::cout << i << '\n';

  return 0;
}

run faster than this?:

#include <iostream>

int main()
{
  while (true)
    for (int i = 0; i < 100; i++)
      std::cout << i << '\n';

  return 0;
}

Maybe the question can be formed this way: Does declaring a variable (without initializing it) cost me resources?

czarson
  • 125
  • 7
  • No. Declaring a variable (of a non-class type) itself does not cost any resources, at least with enabled optimizations (caring about resources with disabled operations makes no sense). Later operations with that variable may cost some resources if they contribute to the observable behavior under the as-if fule. – Daniel Langr Apr 21 '21 at 17:04
  • 1
    Please compare https://godbolt.org/z/j8dr5hsz1 to https://godbolt.org/z/T1YfT63sW, this will show you how the code is compiled on a variety of architectures, and compilers. – SirDarius Apr 21 '21 at 17:05
  • 5
    It's odd to use an infinite loop to ask a question about efficiency. – Mark Ransom Apr 21 '21 at 17:08
  • 1
    Relevant: https://stackoverflow.com/q/7959573/580083. – Daniel Langr Apr 21 '21 at 17:10
  • The infinite loop was supposed to easily represent the fact that int i is getting declared multiple times – czarson Apr 21 '21 at 17:10
  • @czarson that's not how computers works. `int i` is declared only a single time and in your source code. It doesn't exist past that abstraction. – Dan M. Apr 21 '21 at 17:12

2 Answers2

0

Agree with the previos answer. My opinion there is no essentual difference. In both cases the variable will be alocated in the stack. The diffrence is just in variable region of visibility - not important to the performance.

HaronDDC
  • 92
  • 3
  • _"In both cases the variable will be alocated in the stack."_ — That's wrong. With enabled optimizations, the variable value will be mapped to a register. Such as here: https://godbolt.org/z/a5969xbc3 to `ebx`. – Daniel Langr Apr 21 '21 at 17:16
  • 1
    There is no need to provide an additional answer about the fact that you agree. Just add comment to already existing answer. – SergeyA Apr 21 '21 at 17:22
  • @DanielLangr Your are right and not right same time. It depends on the current compiler and platform. You can not be sure that variable is in the registry or it is in the stack. So yes my frase about "stack" was not too accurate. czarson provide us just a sample code. Suppose he analyses some performance issue. Wish him good luck :) – HaronDDC Apr 21 '21 at 17:41
  • @HaronDDC Generally yes, but no sane compiler will store a loop index on the stack with enabled optimizations. – Daniel Langr Apr 21 '21 at 17:54
-1

No, unless you compile with optimizations disabled, in which case the worry about "resources" is strange.

The latter is clearer, and has smaller chance of errors (due to variable having smaller scope), so use that.

Does declaring a variable (without initializing it) cost me resources?

This question is meaningless in general in the presence of the optimizing compilers. For example in your loop, it's possible there won't be "any resource" allocated to this variable in both cases. Don't fall into the trap of the premature optimization. Run code that is clear in intent (both to you and to the compiler), and you'll get good results.

Dan M.
  • 3,818
  • 1
  • 23
  • 41
  • It not clear what optimizations have to do with this. – SergeyA Apr 21 '21 at 17:22
  • For example, with optimizations the compiler might use a register for `i` in both cases. Without it, it may not if it's declared at function level. The point is, you can't really speculate about resource consumption details with optimizations disabled because compilers can be surprisingly dumb to make debugging easier. For example, someone debugging might want to look at the value of `i` after the `for` loop completes. – David Schwartz Apr 21 '21 at 18:02
  • @SergeyA it's impossible to answer for the question as stated. Compilers allowed to do any transforms as long as the "as if" rule is fulfilled. For example, compiler could just fully unroll the loop (unlike for 100 count, unless specifically requested, but still), so there wouldn't even be a need for register/stack space to store the counter. – Dan M. Apr 22 '21 at 11:00