2

I've been wondering about this. For a variable that is used in a very large loop, is it better to initialize it first with a dummy value on top of the loop (outside) or should it be declared and initialized as locally as possible, at the moment it's used inside the loop?

I'm asking because these two posts related to my question seem to give contradictory answers: Declare variables at top of function or in separate scopes?
Should we use temporary variables for the returned values of functions?

One says that it should be declared as locally as possible - I'm assuming this means variables should be declared within loops. The other seems to suggest that local declarations in large loops yield poor performance unless some other special technique is used (I'm not familiar with the example provided by the best answer in this case).

Community
  • 1
  • 1
Manuel
  • 2,143
  • 5
  • 20
  • 22
  • 6
    Define your variables in the innermost scope possible. That way they are available only when they are needed. Enough said. Your compiler will deal with such trivial optimizations. – DeiDei Oct 12 '16 at 22:57
  • Even if this means a significant dip on performance compared to defining variables outside the loop? – Manuel Oct 12 '16 at 22:58
  • 4
    *Your compiler will deal with such trivial optimizations.* – DeiDei Oct 12 '16 at 22:59
  • 1
    Is it a primitive type variable? Is it an object that holds a resource? Will the variable have the same value on each iteration? Your question is too general if you don't provide an example of what you're dealing with. – imreal Oct 12 '16 at 23:02
  • 2
    If you can *measure* (!, with optimization enabled!) a significant difference in performance, go with the faster one. Otherwise, innermost scope it is. – Baum mit Augen Oct 12 '16 at 23:03
  • @Manuel: How did you determine there is a significant performance dip? – GManNickG Oct 12 '16 at 23:17
  • 1
    If the loop is so large that it matters, then you need to make the loop smaller. – William Pursell Oct 12 '16 at 23:19
  • @GManNickG I saw a case example of this in http://stackoverflow.com/questions/13957237/should-we-use-temporary-variables-for-the-returned-values-of-functions. But even if I hadn't seen that post, in theory the constant allocation and deallocation of resources required for a variable declared within its loop surely must add to the overhead of the overall program, right? At least that's what I thought but it seems it isn't as big an issue as I had thought it was. – Manuel Oct 12 '16 at 23:22
  • 3
    @Manuel: You're compiler is *a lot* smarter than you're giving it credit for. :) – GManNickG Oct 12 '16 at 23:41
  • 3
    To reword DeiDei's comment above, if a modern compiler thinks the variable construction will impact performance inside the loop, it will hoist the construction out of the loop for you. This is why it's not seen as a problem. – user4581301 Oct 12 '16 at 23:41
  • How expensive is the type's constructor and/or destructor to call? If it's cheap or no-cost, then it doesn't really matter where you define & initialise. If it's expensive, that's when you should look into optimisation. Also, if you're worried, try telling your compiler to optimise for speed before you try any manual optimisation, you may be surprised at the results. – Justin Time - Reinstate Monica Oct 12 '16 at 23:47
  • @Manuel There is no such overhead. If you're referring to [this answer](http://stackoverflow.com/a/13958126/207421), it's wrong. – user207421 Oct 13 '16 at 01:50

1 Answers1

1

I would think that it would also depend on the context of your environment. Are you talking about turning in an assignment for a class? Turning in a project at work? Personal preference? If this is a case of turning in a program to someone else, if there is an expected way to do it, that's how I would do it.
The other side is that you can always create the variable outside of the loop, and just not initialize it until you get in the loop. That way, if you need it outside of the loop, it's available.