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.

4
  • 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. Commented Jul 23, 2014 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). Commented Jul 23, 2014 at 21:57
  • Agreed. Your loop finishing a millisecond sooner probably isn't worth the headache of intentionally avoiding an incredibly useful language feature. Commented Jul 23, 2014 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. Commented Jul 23, 2014 at 22:14

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

2 Comments

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.
Also I don't think ArrayList.get(i) traverses the list.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.