Declaring/initializing variables inside or outside of a double (or multiple) loop

时间:2015-06-25 19:01:18

标签: java optimization performance-testing nested-loops micro-optimization

According to a similar question, and multiple Google searches, declaring or initializing a variable outside or inside a loop "might" be the same in terms of performance. I am running a computer simulation, and I am trying to optimize the code as much as possible, because it has to run a lot of times and it has multiple inner loops with a lot of iterations. I am wondering if it worth moving all my variables outside of the loops. I have many variables that "theoretically" are constantly re-declared. I said "theoretically" because I don't know if the the Java compiler actually optimize the code and declare everything at, let's say, the method level. Basically, what would be more efficient? CodeA or CodeB? Code A: private void codeA() { double a = 0.0; for (int i1 = 0; i < i1_N; i1++ ) { // Many other loops for (int im = 0; i < im_N; im++ ) { a = 0; // a is used for the first time for (int in = 0; i < in_N; in++ ) { a++; // calculate something with a } } // close many other loops } } Code B: private void codeA() { double a; for (int i1 = 0; i < i1_N; i1++ ) { // Many other loops for (int im = 0; i < im_N; im++ ) { a = 0; // a is used for the first time for (int in = 0; i < in_N; in++ ) { a++; // calculate something with a } } // close many other loops } } Code C: private void codeC() { for (int i1 = 0; i < i1_N; i1++ ) { // Many other loops for (int im = 0; i < im_N; im++ ) { double a = 0; // a is used for the first time for (int in = 0; i < in_N; in++ ) { a++; // calculate something with a } } // close many other loops } } I am just using the variable a in this example, but the computer simulation uses much more. Most of my variables are primitives (int, double, float). Does it make sense to declare primitives outside of the loop in order to improve performance?

1 个答案:

答案 0 :(得分:2)

javac不会优化。所有3个案例is different都生成字节码。 (javac Test.java编译,javap -c Test.class显示字节码)

Java虚拟机可以优化。

JMH基准显示了明显的赢家:

Benchmark           Mode  Cnt    Score    Error  Units
MyBenchmark.testA  thrpt   20  423.437 ±  6.745  ops/s
MyBenchmark.testB  thrpt   20  337.728 ± 56.363  ops/s
MyBenchmark.testC  thrpt   20  351.419 ± 70.290  ops/s

单位是每秒的操作,越多越好。 Benchmark source code。使用OpenJDK IcedTea 2.5.4 Java虚拟机。

因此,在外部声明和初始化变量应该是最有效的。