Do you wonder which of the following methods are the faster way to loop through a list or collection before?
1) While Loop
2) For Loop
3) Iterator Loop
Performance Test – While , For and Iterator
Here i create a simple program to loop through a List with 1,5,10 and 15 millions of data in While loop, For loop and Iterator loop. It’s will calculate and display the elapsed time in output.
Here is the performance test source code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | import java.util.Arrays; import java.util.Date; import java.util.Iterator; import java.util.List; public class ArrayToList { public static void main(String[] argv) { String sArray[] = createArray(); //convert array to list List lList = Arrays.asList(sArray); System.out.println("\n--------- Iterator Loop -------\n"); long lIteratorStartTime = new Date().getTime(); System.out.println("Start: " + lIteratorStartTime); //iterator loop Iterator<String> iterator = lList.iterator(); while ( iterator.hasNext() ){ String stemp = iterator.next(); } long lIteratorEndTime = new Date().getTime(); System.out.println("End: " + lIteratorEndTime); long lIteratorDifference = lIteratorEndTime - lIteratorStartTime; System.out.println("Iterator - Elapsed time in milliseconds: " + lIteratorDifference); System.out.println("\n-------END-------"); System.out.println("\n--------- For Loop --------\n"); long lForStartTime = new Date().getTime(); System.out.println("Start: " + lForStartTime); //for loop for (int i=0; i< lList.size(); i++){ String stemp = (String)lList.get(i); } long lForEndTime = new Date().getTime(); System.out.println("End: " + lForEndTime); long lForDifference = lForEndTime - lForStartTime; System.out.println("For - Elapsed time in milliseconds: " + lForDifference); System.out.println("\n-------END-------"); System.out.println("\n--------- While Loop -------\n"); long lWhileStartTime = new Date().getTime(); System.out.println("Start: " + lWhileStartTime); //while loop int j=0; while (j< lList.size()) { String stemp = (String)lList.get(j); j++; } long lWhileEndTime = new Date().getTime(); System.out.println("End: " + lWhileEndTime); long lWhileDifference = lWhileEndTime - lWhileStartTime; System.out.println("While - Elapsed time in milliseconds: " + lWhileDifference); System.out.println("\n-------END-------"); } static String [] createArray(){ String sArray[] = new String [15000000]; for(int i=0; i<15000000; i++) sArray[i] = "Array " + i; return sArray; } } |
Output
D:\test>java -Xms1024m -Xmx1024m ArrayToList --------- Iterator Loop ------- Start: 1232435614372 End: 1232435614763 Iterator - Elapsed time in milliseconds: 390 -------END------- --------- For Loop -------- Start: 1232435614763 End: 1232435614997 For - Elapsed time in milliseconds: 234 -------END------- --------- While Loop ------- Start: 1232435614997 End: 1232435615232 While - Elapsed time in milliseconds: 234 -------END-------
Performance Test Result (in milliseconds)

Conclusion
Well… the result show Iterator mechanism is the slowest method to loop through a list. There’s not much performance different between For and While loop.
Iterator provides a very handy way to loop through a list or collection, but it is slower than For and While loop. Please be remind that the different is just in milliseconds (not even 1 second for 15 millions of data looping). So, just choose any looping mechanism you want, there’s not much performance different.
P.S 15 millions of data and java -Xms1024m -Xmx1024m is the limit of my computer , may be someone can help me test this in 20 millions of data? Thanks ~



[...] [upmod] [downmod] While loop, For loop and Iterator Performance Test – Java | Java (mkyong.com) 1 points posted 3 months, 3 weeks ago by SixSixSix tags java imported saved by [...]
Ugh, yet more micro benchmarks.
THEY DON’T WORK.
[...] example, some days ago a ‘performance test’ on while loops, iterators and for loops was posted. This test is wrong and inaccurate. I will use this test as an example, but there are [...]
public static void test2(String[] lst)
{
int size = (lst.length & -1);
int i = 0;
while (i < size)
{
String s;
s = lst[i++];
s = lst[i++];
}
while (i < lst.length)
{
String s = lst[i];
}
}
public static void test2(String[] lst) {
int size = (lst.length & -2);
int i = 0;
while (i < size) {
String s;
s = lst[i++];
s = lst[i++];
}
while (i < lst.length) {
String s = lst[i++];
}
}
can you give some explanation here about what are you trying to say?
I had been told that looping through a loop backwards is faster, since you are always comparing to 0 which is apparently faster, but i’m not sure how accurate that is. Tests I have done don’t really show any improvement.
yes i agree with you, here is the testing i made for loop backward
http://www.mkyong.com/java/reverse-loop-versus-forward-loop-in-performance-java/
Thanks for the stats.
Try changing the order of the loops in your code and see if that changes their times
Thanks for tips, ya… it did change result a bit, however the stats i show is taking by the 5 execution average time. So the result should be correct.
Why Many Java Performance Tests are Wrong
http://java.dzone.com/articles/why-many-java-performance-test
My favorite loop is: for(int i=0, iMax=list.size(); i<iMax; i++) …
I wonder how that compares to an iterator loop, given that you take the list size method out of the loop.
sorry , can you elaborate more about it?