4

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.

startoftext
  • 3,846
  • 7
  • 40
  • 49
  • Good question. I haven't worked on android for years, but when I did, I wondered the same thing. I would hope it is obsolete advice by now. – Carl Manaster Jul 23 '14 at 21:54
  • 2
    Unless you're doing very computationally intensive loops, I'd avoid worrying about it altogether and just go for the readable approach (in my opinion, that is `foreach` loops). – Kevin Coppock Jul 23 '14 at 21:57
  • Agreed. Your loop finishing a millisecond sooner probably isn't worth the headache of intentionally avoiding an incredibly useful language feature. – Veselin Romić Jul 23 '14 at 22:06
  • 1
    I understand the answer to this has little practical value. I think it would only matter for really large lists. Some times I just wonder what the answer is even though it might not change how I do stuff. – startoftext Jul 23 '14 at 22:14

2 Answers2

1

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 ;)

Community
  • 1
  • 1
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
  • According to the android docs that I linked to I think they are saying for(i=0... style loops are slower on android. Again this is specific to Android. Also I am not asking because I am concerned about it slowing my apps down. The performance difference for most list sizes probably does not matter AT ALL. Its just something that I was curious about. – startoftext Jul 23 '14 at 22:12
  • 1
    Also I don't think ArrayList.get(i) traverses the list. – startoftext Jul 23 '14 at 22:20
1

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.