Reputation: 3916
I have read the performance tips here: http://developer.android.com/training/articles/perf-tips.html#Loops and at the time it looks like
for(i=0; i <= objectArrayList.size() ; ++i){}
loops are preferred for performance reasons to "for each" or "enhanced" style loops
for(Object object : objectArrayList){}
Does this still hold true for ART or will that change things? Just wondering.
Upvotes: 4
Views: 215
Reputation: 30875
The difference between Dalvik and ART can be really simplified to point that. Dalvik is JIT (Just-in-Time) and ART is AOT (Ahead-of-Time). This refer too the generation of executable code. So all guides that ware valid for dalvik are also valid for ART.
In your case with ArrayList
, a better solution in term of memory allocation is the counted loop as you do not create additional instance for iterator. But in term of code maintenance the enhanced for is easier.
The guides that are currently used for Android developers ware wrote few year ago. The are updated but in case you write on device that support android K, this kind of optimization may be classified as premature.
Upvotes: 1
Reputation: 122026
Not an Android guru :)
Seems a premature Optimization to me. Well you have a valid question though. See
for(i=0; i <= objectArrayList.size() ; ++i){
Object o = objectArrayList.get(i);
/// do stuff
}
So it traverse each time the list to get that particular element. Where as
for(Object object : objectArrayList){
// do stuff
}
Uses iterator
and just a bit faster than the normal for
. Steps over to next element.
Though I prefer for-each
because of readability, since all recent jvm's are super faster ;)
Upvotes: 1