Difference between declaring variables before or in loop?

前端 未结 25 1993
长发绾君心
长发绾君心 2020-11-22 02:37

I have always wondered if, in general, declaring a throw-away variable before a loop, as opposed to repeatedly inside the loop, makes any (performance) difference? A (q

相关标签:
25条回答
  • 2020-11-22 03:07

    It depends on the language and the exact use. For instance, in C# 1 it made no difference. In C# 2, if the local variable is captured by an anonymous method (or lambda expression in C# 3) it can make a very signficant difference.

    Example:

    using System;
    using System.Collections.Generic;
    
    class Test
    {
        static void Main()
        {
            List<Action> actions = new List<Action>();
    
            int outer;
            for (int i=0; i < 10; i++)
            {
                outer = i;
                int inner = i;
                actions.Add(() => Console.WriteLine("Inner={0}, Outer={1}", inner, outer));
            }
    
            foreach (Action action in actions)
            {
                action();
            }
        }
    }
    

    Output:

    Inner=0, Outer=9
    Inner=1, Outer=9
    Inner=2, Outer=9
    Inner=3, Outer=9
    Inner=4, Outer=9
    Inner=5, Outer=9
    Inner=6, Outer=9
    Inner=7, Outer=9
    Inner=8, Outer=9
    Inner=9, Outer=9
    

    The difference is that all of the actions capture the same outer variable, but each has its own separate inner variable.

    0 讨论(0)
  • 2020-11-22 03:07

    In my opinion, b is the better structure. In a, the last value of intermediateResult sticks around after your loop is finished.

    Edit: This doesn't make a lot of difference with value types, but reference types can be somewhat weighty. Personally, I like variables to be dereferenced as soon as possible for cleanup, and b does that for you,

    0 讨论(0)
  • 2020-11-22 03:08

    I would always use A (rather than relying on the compiler) and might also rewrite to:

    for(int i=0, double intermediateResult=0; i<1000; i++){
        intermediateResult = i;
        System.out.println(intermediateResult);
    }
    

    This still restricts intermediateResult to the loop's scope, but doesn't redeclare during each iteration.

    0 讨论(0)
  • 2020-11-22 03:08

    There is a difference in C# if you are using the variable in a lambda, etc. But in general the compiler will basically do the same thing, assuming the variable is only used within the loop.

    Given that they are basically the same: Note that version b makes it much more obvious to readers that the variable isn't, and can't, be used after the loop. Additionally, version b is much more easily refactored. It is more difficult to extract the loop body into its own method in version a. Moreover, version b assures you that there is no side effect to such a refactoring.

    Hence, version a annoys me to no end, because there's no benefit to it and it makes it much more difficult to reason about the code...

    0 讨论(0)
  • 2020-11-22 03:09

    It's an interesting question. From my experience there is an ultimate question to consider when you debate this matter for a code:

    Is there any reason why the variable would need to be global?

    It makes sense to only declare the variable once, globally, as opposed to many times locally, because it is better for organizing the code and requires less lines of code. However, if it only needs to be declared locally within one method, I would initialize it in that method so it is clear that the variable is exclusively relevant to that method. Be careful not to call this variable outside the method in which it is initialized if you choose the latter option--your code won't know what you're talking about and will report an error.

    Also, as a side note, don't duplicate local variable names between different methods even if their purposes are near-identical; it just gets confusing.

    0 讨论(0)
  • 2020-11-22 03:09

    Tried the same thing in Go, and compared the compiler output using go tool compile -S with go 1.9.4

    Zero difference, as per the assembler output.

    0 讨论(0)
提交回复
热议问题