/*
* How To Write Faster Loops (after Dan Bornstein, Google Engineer)
*
* - http://www.youtube.com/watch?v=ptjedOZEXPM
*
*/
/* 1 (fastest) */
for (int i = initializer; i >= 0; i--) { ... }
/* 2 */
int limit = calculateLoopLimit();
for (int i = 0; i < limit; i++) { ... }
/* 3 */
Type[] array = getMyArray();
for (Type obj : array) { ... }
/* 4 */
for (int i = 0; i < array.length; i++) { ... }
/* 5 */
for (int i = 0; i < this.var; i++) { ... }
/* 6 */
for (int i = 0; i < obj.size(); i++) { ... }
/* 7 (slowest) */
Iterable<Type> list = getMyList();
for (Type obj : list) { ... }
17 Comments
Actually,
for (int i = initializer; i >= 0; --i) { ... }
is faster
i second that :) although i come to think, does it matter in java?
Actually,
your compiler should turn this into exactly the same code. If it does not you should switch compilers.
The whole post is pretty misleading anyway, since most of the time you do substantial work inside the loop and thus should pick the variant that causes the least overhead in that code...
Does it make no difference what language is used, or why otherwise have you not mentioned that detail?
Assuming the compiler doesn't already notice this optimization, wouldn't
int i = size
while (i-- > 0) { ... }
be faster than the #1 entry?
To the people talking about "changing compilers" or "will the compiler optimize this", please note that this post is talking about Dalvik on Android, not a classic Java compiler. The rules are different.
Having said that, I sure hope future generations of Dalvik can optimize these sorts of things. This reminds me of the JDK 1.0 days.
Disregard my comment above, Dan Morrill set me straight on Twitter http://twitter.com/morrildl/status/1505732758
for (int i = 0; i < this.var; i++) { ... }
This is the fastest way.
Can anybody post the timestamp for the related video when Bornstein is talking about the loop performance?
I am curious why the '--i'-loop should be faster than '++i'-loop, but I do not like to assist the entire video. I would expect that both loops are identical in execution time.
I think it is faster because of the comparison with 0, which takes just 1 cpu cycle. The comparison with another variable is more expensive...
The fastest loop via --i has to do with how dalvik treats stuff..
remember in normal java VM terms a loop with --i does a certain thing to the stack..ie pop?
those moves on and off the stack are optimized in dalvik and thus the loop with the less moves on and off the simulated stack in java bytecode will of course be faster in dalvik.
These loops are something that we're all used to, so am not sure there's anything new there.
That said, this still seems ok #
# for (int i = 0; i < array.length; i++) { ... }
Is this slower, faster or same as 7?
for(Iterator<T> it=list.iterator(); it.hasNext(); ){
T t = it.next();
...
}
@ma_as
slower! you call two functions in your loop clauses. That's always a bad idea.
And keep in mind that iterators cause additional object creation. So I consider them evil when performance counts.
See also http://developer.android.com/guide/practices/design/performance.html#cache_fields
cache what can be cached... even field lookups.
Thanks for sharing
Faster. Love it. Thanks
Great info.
Add a Comment