Difference between declaring variables before or in loop?

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

    It is language dependent - IIRC C# optimises this, so there isn't any difference, but JavaScript (for example) will do the whole memory allocation shebang each time.

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

    As a general rule, I declare my variables in the inner-most possible scope. So, if you're not using intermediateResult outside of the loop, then I'd go with B.

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

    Which is better, a or b?

    From a performance perspective, you'd have to measure it. (And in my opinion, if you can measure a difference, the compiler isn't very good).

    From a maintenance perspective, b is better. Declare and initialize variables in the same place, in the narrowest scope possible. Don't leave a gaping hole between the declaration and the initialization, and don't pollute namespaces you don't need to.

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

    The following is what I wrote and compiled in .NET.

    double r0;
    for (int i = 0; i < 1000; i++) {
        r0 = i*i;
        Console.WriteLine(r0);
    }
    
    for (int j = 0; j < 1000; j++) {
        double r1 = j*j;
        Console.WriteLine(r1);
    }
    

    This is what I get from .NET Reflector when CIL is rendered back into code.

    for (int i = 0; i < 0x3e8; i++)
    {
        double r0 = i * i;
        Console.WriteLine(r0);
    }
    for (int j = 0; j < 0x3e8; j++)
    {
        double r1 = j * j;
        Console.WriteLine(r1);
    }
    

    So both look exactly same after compilation. In managed languages code is converted into CL/byte code and at time of execution it's converted into machine language. So in machine language a double may not even be created on the stack. It may just be a register as code reflect that it is a temporary variable for WriteLine function. There are a whole set optimization rules just for loops. So the average guy shouldn't be worried about it, especially in managed languages. There are cases when you can optimize manage code, for example, if you have to concatenate a large number of strings using just string a; a+=anotherstring[i] vs using StringBuilder. There is very big difference in performance between both. There are a lot of such cases where the compiler cannot optimize your code, because it cannot figure out what is intended in a bigger scope. But it can pretty much optimize basic things for you.

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

    A co-worker prefers the first form, telling it is an optimization, preferring to re-use a declaration.

    I prefer the second one (and try to persuade my co-worker! ;-)), having read that:

    • It reduces scope of variables to where they are needed, which is a good thing.
    • Java optimizes enough to make no significant difference in performance. IIRC, perhaps the second form is even faster.

    Anyway, it falls in the category of premature optimization that rely in quality of compiler and/or JVM.

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

    I think it depends on the compiler and is hard to give a general answer.

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