Java speed access array index versus temp variable

前端 未结 6 814
情深已故
情深已故 2021-01-17 13:21

What is faster in Java. Accessing an array index directly multiple times, or saving the value of the array index to a new variable and use this for following compution?

6条回答
  •  广开言路
    2021-01-17 14:11

    Run the code through a profiler to answer the question for your usecase.

    The answer to this will probably be JVM specific. The Oracle HotSpot JVM is going to perform differently than OpenJDK or IBM's JDK. Timings will depend on how the JVM optimizes the byte code, what it decides to compile while it is running. Server vs client mode will probably make a difference too.

    So aim for readability. Optimize after profiling and determining that section of code is the problem.

提交回复
热议问题