Why iterators should not be used ... ?
Iterator's compared to for and while loops ... A performance test
Iterator provides a very handy way to loop through a list or collection, but it is slower than For and While loop.
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-------
No comments:
Post a Comment