In Android development, only a list-style interface, we almost all need to use list to store data, in a very small list words almost no matter what kind of loop traversal in general performance is no different. However, when we encounter a slightly larger amount of data, it is necessary to consider which method of writing is more high performance.
The following three types are common:
First Kind
for (String s : tests) { // ....}
Another kind
sizetests.size();fori0isizei{ tests.get(i);}
Third Kind
it = tests.iterator(); while(it.hasNext()) { it.next(); }
The use of the above three in the case of data volume less than or equal to 100 is almost the output time is 0, when around 1000 the third kind of slightly slower around 1ms < may be in the environmental reasons > can say three ways is no difference, When the amount of data around 10000 we can see the difference between the first and the third almost identical to 4ms and the other is only 2ms, when the data volume in 100000 or so the first kind of almost the same 40ms, another 17ms, a third 33ms.
Statement: The above environment in the MacBook Pro, Eclipse-Android5.0-NEXUS5 mobile phone environment test results, Google below the data show in the case of non-JIT in fact the first is the fastest, But in the Android environment or another one is the best performance choice.
List Loop traversal performance comparison in Android