Do you wonder which of the following methods are the fastest 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 records in While loop, For loop and Iterator loop.

Here’s the source code to run the simulating the testing date.

package com.mkyong.core;
 
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 that the 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 different.

Note
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 ~
Tags :
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider making a donation to the charity, thanks.

Related Posts

Popular Posts