Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

for loop question

 
Chris Shepherd
Ranch Hand
Posts: 286
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would like to know if a for loop like the one that follows calls the array length once and then holds the value to check against for all its iterations, or if it calls the array length every time it loops through.

Thanks.
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Chris,
It would have to call it each time. What if myarray is referenced to a different array inside the loop? Now that isn't to say that some optimizing slight of hand might take place to replace myarray.length if the context guaranteed that myarray would not change inside the block.

Michael Morris
 
Gabriel White
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It doesn't necesscarily call the array length everytime. This is a for loop, thus it is a counter loop. The loop will increment through the array's indexes until it reaches the array.length.
Its just like putting the number 5 in there instead of array.length. Does this access the number 5 everytime? It does in a way. It simply checks to see if the int is smaller,larger,etc than that int value, but it does not parse the entire array. The for loop knows that this is just a number to be referenced by the initial int value.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The length variable here is a member of the array (kind of). As mentioned earlier, the array itself my change inside the loop, so yes the variable has to be evaluated every time. This is especially important to understand if you were to use a function call instead of a field name. In the case of a function, the function will be called EVERY time the loop iterates. If the function takes a lot of time, then this time factor is basically squared by the loop.
I hope this makes some sense and answers your question. In short, yes the variable is accessed every time the loop iterates.
Layne
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Its just like putting the number 5 in there instead of array.length.

Not True. Try this:

Michael Morris
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's easy to optimize this by hand to guarantee that no unnecessary lookup is performed each time thorough the loop:

That said, even if you don't do this and keep the loop in the form Chris originally showed, it's entirely possible that HotSpot could detect that (a) the .length attribute is a constant, and (b) myArray is never actually reassigned to another value inside the loop. In this case HotSpot could well optimize the loop to mirror the form I showed above. The chance of this occuring probably increases if myArray is declared as final. And it's also possible this optimization could be preformed by javac rather than HotSpot - but my impression is that Sun hasn't put that much effort into getting javac to optimize; there's much more benefit to putting effort into HotSpot.
Of course, if the code does change myArray, as Michael showed, then these optimizations will not occur, and myArray.length must be looked up each time through the loop.
 
Gabriel White
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the info. This forum rulez
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic