7

I've recently been reviewing code an noticed the use of this syntax in a for loop

for(int i = 0, len = myArray.length; i < len; i++){
    //some code
}

as opposed to:

for(int i = 0; i < myArray.length; i++){
    //some code
}

With the reasoning that it is more efficient as you don't have to keep looking up the myArray.length property with each loop.

I created a test to check if this was the case, and in all my tests the first for loop approach was significantly (around 70%) faster than the second.

I was curious why this syntax isn't more widely adopted and that i'm correct in thinking it is a better approach to using a for loop through an array.

Loco234
  • 521
  • 4
  • 20
  • 2
    "I was curious why this syntax isn't more widely adopted and that i'm correct in thinking it is a better approach to using a for loop through an array." Because most people use iterators today :-) – Smutje Apr 28 '15 at 10:39
  • 2
    IMHO the first syntax is not widely used because it is a micro optimization, if you need to squeeze out that amount of performance probably you have to rethink a lot of things, and a `for` loop might be the last. – Alberto Zaccagni Apr 28 '15 at 10:40
  • That's a good point. Even i dislike using .size() and .length() methods in for loop. More acceptable approach would be for(String str: myarray){} – raki Apr 28 '15 at 10:42
  • Remember that the first will come unstuck if the array length changes within the loop. – Component 10 Apr 28 '15 at 10:46
  • The case for reading the length once in loop initialization rather than in each iteration is stronger when the length is read from a method call (e.g. list.size()) rather than a field access (array.length). – ewan.chalmers Apr 28 '15 at 10:48
  • Can we see the test code please? Is the result [statistical significant](http://en.wikipedia.org/wiki/Statistical_significance)? Did you follow [proper benchmark settings](http://stackoverflow.com/q/504103/572670)? – amit Apr 28 '15 at 10:50
  • _"I created a test to check if this was the case, and in all my tests the first for loop approach was significantly (around 70%) faster than the second." _ Could you tell more about it? very unlikely on normal conditions. Did you try with JMH (http://openjdk.java.net/projects/code-tools/jmh/) – Jayan Apr 28 '15 at 11:09

3 Answers3

5

This is a caching optimization, preventing, as you noted, many reads of length. Frequently, it's not necessary to perform this kind of micro-optimization, which may explain why most engineers are happy to examine length after each loop instead of caching it off, but it's useful in high-performance code. It can also be written like this, inverting the loop's direction and avoiding the use of another variable to hold the array's length:

for (int i = myArray.length - 1; i >= 0; i--) {
    // some code
}
Chris Mantle
  • 6,595
  • 3
  • 34
  • 48
0

The first approach evaluate the length only one time instead of the second where the length is evaluated each loop.

Steph
  • 779
  • 1
  • 8
  • 18
  • There is no computation there, only field access, which will pretty likely be optimized anyway. – amit Apr 28 '15 at 10:43
  • I changed the word compute to evaluate but in my mind JVM will optimize this – Steph Apr 28 '15 at 10:47
0

In my opinion, most of the Java programmers go with for-each loop in case of arrays unless we want to do some manipulation with the index values

HJK
  • 1,382
  • 2
  • 9
  • 19