Optimization for loops with ArrayLists

From this article ,

manually counted loop about 3 times faster

than a reinforced loop for iterating over arrays.

First, what do they mean by “handwritten counting loop”? They clearly did not specify what this means. Secondly, why is this true only for arraialists, and not for other collections?

+5
source share
4 answers

First, what do they mean by “manual counting loop”?

I guess they mean

for(int i=0;i<list.size();i++) {
    list.get(i);
}

Secondly, why is this true only for arraialists, and not for other collections?

ArrayList Iterator . ( , , )

, . LinkedList, Iterator, get(n) . Set get(n)

+6

( , , ), .

Android !

for(int i=0;i<list.size();i++) {
    list.get(i);
}

!

( ):

int size = list.size();
for(int i=0;i<size;i++) {
    list.get(i);
}

! , , , Google .

+3

zero() one() :

List<Foo> foos = new ArrayList<Foo>();
// populate the list...

for (int i=0; i<foos.size(); i++) {
    Foo foo = foos.get(i);
    // do something with foo
}
0

This is a handwritten counting cycle. What they mean is that you manually wrote a loop that counts every element in the array.

for (int i = 0; i < mArray.length; ++i) {

This is true for arraists, given their basic implementation. Since the extended loop should work in the general case, it cannot accept certain things; such as the start and end points of the index.

0
source

All Articles