If you need to loop over a static collection it is common to write :
for (int i = 0; i < items.length; i++)
{
...... //do something
}
but if your items collection is very large this code has a small overhead. Because you have to check the length property in every steps and because it uses a reference to the length property it brings a performance penalty. For this situation I used the technique below:
for (int i = 0, len = items.length; i < len; i++)
{
...... //do something
}
This way you just check the items.length property once.
For Loop
Subscribe to:
Post Comments (Atom)
1 comments:
...or, you could just declare the values before entering the loop :D
But that's how the compiler typically does it..? Inserting the "check" into every part of the loop, and runs the declaration just once. So you'd avoid pushing that lookup to the recurring loop?
Post a Comment