0

So I have an application which does quite a lot of math, therefore there's a big amount temp variables. The thing I'm wondering: is there any performance difference when I declare variable before a loop and then reuse it (code 1), or is it better to declare it inside the loop (code 2)? The variable is needed only inside the loop and the n can be up to tens of thousands. Or is it better to have them as global variables? The loop is also called more than once.

code 1:

double temp;
for (int i = 0; i < n; i++) {
   temp = ...;
}

code 2:

for (int i = 0; i < n; i++) {
  double temp = ...;
}

Thanks for tips.

Glissinda
  • 169
  • 1
  • 9
  • The 1st part code 1 seems to be logical and never ever use global variables in your code as they can cause lot of unwanted results – vikeng21 Mar 31 '15 at 15:11
  • 1
    What do your benchmarks show? – resueman Mar 31 '15 at 15:11
  • 2
    Modern JVM will automatically detect when a piece of code is called sevral times and will perform deep optimization and compilation to improve performance when running the block of code. So you should not bother to much with that and let JVM do the job for you. – ortis Mar 31 '15 at 15:18

3 Answers3

9

No, it's no difference in performance. (Neither computational nor memory wise.) The declaration isn't "executed" in runtime.

In fact, the following two snippets generate the exact same bytecode:

// Snippet 1                        // Snippet 2
int i;                              int i;
double temp;                        for (i = 0; i < 5; i++) {     
for (i = 0; i < 5; i++) {               double temp = i;
    temp = i;                       }
}

// Generates                        // Generates

   0: iconst_0                       0: iconst_0
   1: istore_1                       1: istore_1
   2: iload_1                        2: iload_1
   3: iconst_5                       3: iconst_5
   4: if_icmpge     16               4: if_icmpge     16
   7: iload_1                        7: iload_1
   8: i2d                            8: i2d
   9: dstore_2                       9: dstore_2
  10: iinc          1, 1             10: iinc          1, 1
  13: goto          2                13: goto          2

So, that should answer your question regarding performance.

Now, I would still opt for the second approach, since it limits the scope of temp, i.e. it clutters the namespace the least.

aioobe
  • 413,195
  • 112
  • 811
  • 826
  • Just a small addition: once this loop is compiled into machine code, it will be eliminated completely. So even if the byte code wasn't identical, any naive approaches to microbenchmarking to establish relative speed would've failed. – biziclop Mar 31 '15 at 15:27
  • Yep. Micro-benchmarking this type of thing would yield completely useless results. – aioobe Mar 31 '15 at 15:33
2

It's generally better to declare variables in the smallest scope possible (inside of the loop). That way object references will be eligible for garbage collection earlier, and so your code will be more memory efficient. Primitive variables will be freed when the method they are declared in leaves the stack anyway, but it is still good practice to declare them in as limited a scope as possible.

As for speed considerations, it shouldn't make any difference. Byte code analysis in the these answers, shows that ultimately there is no difference.

References

Community
  • 1
  • 1
Rudi Kershaw
  • 12,332
  • 7
  • 52
  • 77
0

It is hard to say if there is a performance difference in the two, you can measure it, but I imagine you would not find much of a difference (Really depends on what your doing and your compiler)

However, I think code option 2 is better practice because its only used in the loop, so you do not need any wider scoping outside of the loop for that variable.

JarODirt
  • 157
  • 11