How To Write Faster Loops

2 votes · 12 comments

According to the DalvikVM designer Dan Bornstein these are the fastest ways of looping, sorted from fast to slow. Found on anddev

raw ·
copy
· download
/* * 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) { ... }
Add a comment

12 Comments

Actually,

for (int i = initializer; i >= 0; --i) { ... }

is faster

Reply · April 2, 2009, 6:17 a.m.

i second that :) although i come to think, does it matter in java?

Reply · April 2, 2009, 6:24 a.m.

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...

Reply · April 2, 2009, 6:37 a.m.

Does it make no difference what language is used, or why otherwise have you not mentioned that detail?

Reply · April 2, 2009, 10:27 a.m.

Assuming the compiler doesn't already notice this optimization, wouldn't

int i = size while (i-- > 0) { ... }

be faster than the #1 entry?

Reply · April 2, 2009, 11:14 p.m.

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.

Reply · April 13, 2009, 6:13 a.m.

Disregard my comment above, Dan Morrill set me straight on Twitter http://twitter.com/morrildl/status/1505732758

Reply · April 13, 2009, 8:30 a.m.

for (int i = 0; i < this.var; i++) { ... } This is the fastest way.

Reply · May 3, 2009, 2:13 p.m.

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...

Reply · May 20, 2009, 5:51 a.m.

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.

Reply · May 30, 2009, 9:22 p.m.

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++) { ... }

Reply · Aug. 17, 2009, 1:30 a.m.

Is this slower, faster or same as 7?

for(Iterator it=list.iterator(); it.hasNext(); ){ T t = it.next(); ... }

Reply · Aug. 31, 2009, 2:17 a.m.