Difference between declaring variables before or in loop?

前端 未结 25 2071
长发绾君心
长发绾君心 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:19

    From a performance perspective, outside is (much) better.

    public static void outside() {
        double intermediateResult;
        for(int i=0; i < Integer.MAX_VALUE; i++){
            intermediateResult = i;
        }
    }
    
    public static void inside() {
        for(int i=0; i < Integer.MAX_VALUE; i++){
            double intermediateResult = i;
        }
    }
    

    I executed both functions 1 billion times each. outside() took 65 milliseconds. inside() took 1.5 seconds.

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

    My practice is following:

    • if type of variable is simple (int, double, ...) I prefer variant b (inside).
      Reason: reducing scope of variable.

    • if type of variable is not simple (some kind of class or struct) I prefer variant a (outside).
      Reason: reducing number of ctor-dtor calls.

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

    Well, you could always make a scope for that:

    { //Or if(true) if the language doesn't support making scopes like this
        double intermediateResult;
        for (int i=0; i<1000; i++) {
            intermediateResult = i;
            System.out.println(intermediateResult);
        }
    }
    

    This way you only declare the variable once, and it'll die when you leave the loop.

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

    I suspect a few compilers could optimize both to be the same code, but certainly not all. So I'd say you're better off with the former. The only reason for the latter is if you want to ensure that the declared variable is used only within your loop.

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

    A) is a safe bet than B).........Imagine if you are initializing structure in loop rather than 'int' or 'float' then what?

    like

    typedef struct loop_example{
    
    JXTZ hi; // where JXTZ could be another type...say closed source lib 
             // you include in Makefile
    
    }loop_example_struct;
    
    //then....
    
    int j = 0; // declare here or face c99 error if in loop - depends on compiler setting
    
    for ( ;j++; )
    {
       loop_example loop_object; // guess the result in memory heap?
    }
    

    You are certainly bound to face problems with memory leaks!. Hence I believe 'A' is safer bet while 'B' is vulnerable to memory accumulation esp working close source libraries.You can check usinng 'Valgrind' Tool on Linux specifically sub tool 'Helgrind'.

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

    This is a gotcha in VB.NET. The Visual Basic result won't reinitialize the variable in this example:

    For i as Integer = 1 to 100
        Dim j as Integer
        Console.WriteLine(j)
        j = i
    Next
    
    ' Output: 0 1 2 3 4...
    

    This will print 0 the first time (Visual Basic variables have default values when declared!) but i each time after that.

    If you add a = 0, though, you get what you might expect:

    For i as Integer = 1 to 100
        Dim j as Integer = 0
        Console.WriteLine(j)
        j = i
    Next
    
    'Output: 0 0 0 0 0...
    
    0 讨论(0)
提交回复
热议问题