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 ~