In Android development, as long as the list-style interface we need to use lists to store data, in a very small list of almost any kind of loop traversal mode overall performance is no different, but when we encounter a slightly larger amount of data, it is necessary to consider which way to write more high performance.
The following three types are common:
First Kind
for (String s : tests) { // ....}
The second Kind
sizetests.size();fori0isizei{ tests.get(i);}
Third Kind
it = tests.iterator(); while(it.hasNext()) { it.next(); }
Using the above three in the case of data volume less than or equal to 100, almost the output time is 0, when around 1000 of the time the third kind of slightly slower around 1ms < may be at the time of environmental reasons > can say three ways is also no difference, When the amount of data around 10000 we can see the difference between the first and the third kind of almost 4ms and the second as long as 2ms, when the amount of data in 100000 or so, the first kind of almost 40ms, the second 17ms, the third kind of 33ms.
Disclaimer: The above environment in the MacBook Pro, Eclipse-Android5.0, NEXUS5 mobile phone environment test results, Google the following information shows in the case of non-JIT in fact, the first is the fastest, But in the Android environment, the second is the best performance option.
List Loop traversal performance comparison in Android