0

I just recently finished an android project and I have done some extensive codes of ArrayList and doing looping stuffs on it. I have two sets example of loops here.

ArrayList<SomeObject> object = new ArrayList<SomeObject>();

for(int c=0;c<object.size();c++){

}

for(SomeObject obj: object){

}

I think the second one is good if you don't need the incrementing value and I think far more readable and neat than the first. But what in the two loops is ideal or efficient to use in terms speed, performance and memory usage?

6
  • 1
    You're trying to out guess the JVM and that's nothing but a losing game. So don't do it. Please before you go further, read this article on writing dumb code. Commented Feb 24, 2013 at 6:58
  • Dalvik optimization may differ from other JVMs, so Android is valid Commented Feb 24, 2013 at 6:58
  • 3
    Opt for readable over questionable performance gains. The JVM is smarter than you give it credit for :) Commented Feb 24, 2013 at 6:58
  • You should first profile it. Commented Feb 24, 2013 at 6:59
  • 3
    checkout developer.android.com/training/articles/perf-tips.html#Loops Commented Feb 24, 2013 at 7:02

1 Answer 1

1

As per suggestions from the Android Documentation the most efficient ways for making loop are:

public void one() {
    int sum = 0;
    Foo[] localArray = mArray;
    int len = localArray.length;

    for (int i = 0; i < len; ++i) {
        sum += localArray[i].mSplat;
    }
}

public void two() {
    int sum = 0;
    for (Foo a : mArray) {
        sum += a.mSplat;
    }
}

Please, note that these ways have same performance for devices with JIT. So, in cases then JIT is off (e.g. while debugging using Eclipse) You might observe different results (two() will be faster than one())

Update
I've checked ArrayLists also by using the following code:

    final ArrayList<String> testArrayList = new ArrayList<String>(1000);

    for (int i = 0; i < 1000; i++) {
        testArrayList.add(String.valueOf(i<<2));
    }

    final TimingLogger timings = new TimingLogger("Loop", "ArrayLists");

    timings.addSplit("start");

    for (int loopCount = 0; loopCount < 1000; loopCount++) {
        int sum = 0;

        for (int i = 0; i < testArrayList.size(); i++) {
            sum += Integer.valueOf(testArrayList.get(i));
        }
    }

    timings.addSplit("zero()");

    for (int loopCount = 0; loopCount < 1000; loopCount++) {
        int sum = 0;
        final int len = testArrayList.size();

        for (int i = 0; i < len; i++) {
            sum += Integer.valueOf(testArrayList.get(i));
        }
    }

    timings.addSplit("one()");

    for (int loopCount = 0; loopCount < 1000; loopCount++) {
        int sum = 0;

        for (String item : testArrayList) {
            sum += Integer.valueOf(item);
        }
    }

    timings.addSplit("two()");
    timings.dumpToLog();

And obtained the following output (with JIT):

ArrayLists: begin
ArrayLists:      0 ms, start
ArrayLists:      2713 ms, zero()
ArrayLists:      1753 ms, one()
ArrayLists:      1922 ms, two()
ArrayLists: end, 6388 ms

So, we able to see, that variants one() and two() provides similar results, and these results are faster than zero() (so, results look similar to ones described in the documentation for arrays).

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

5 Comments

The question isn't about arrays, it is about ArrayList. Not relevant.
Do you think Arrays are not relevant to ArrayList or has significant performance diffences then using in loops?
Updated the answer with my measurements on ArrayLists.
Thank you for this @sandrstar. Did you do this in eclipse? is JIT a plugin in eclipse?
I've created project in eclipse to profile. JIT is not part of eclipse, checkout android-developers.blogspot.kr/2010/05/dalvik-jit.html here for some brief description of it.

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.