PDA

View Full Version : Stop using for each loops



TheBuzzer
15 Dec 2009, 11:01 AM
I just ran a test, it seems like for each loop is slow. a normal for loop is faster.

no clue why but even in javac that case happens



package com.ottocap.NewWorkFlow.client.Widget;

import java.util.Arrays;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

public class ArrayToList {
public ArrayToList() {

String sArray[] = createArray();

//convert array to list
List lList = Arrays.asList(sArray);

System.out.println("\n--------- For Each Loop -------\n");
long lForEachStartTime = new Date().getTime();
System.out.println("Start: " + lForEachStartTime);

//for loop
for (Object thestring: lList){
String stemp = (String)thestring;
}

long lForEachEndTime = new Date().getTime();
System.out.println("End: " + lForEachEndTime);

long lForEachDifference = lForEachEndTime - lForEachStartTime;
System.out.println("ForEach - Elapsed time in milliseconds: " + lForEachDifference);

System.out.println("\n-------END-------");


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 [150000];

for(int i=0; i<150000; i++)
sArray[i] = "Array " + i;

return sArray;
}
}


--------- For Each Loop -------

Start: 1260903435636
End: 1260903435648
ForEach - Elapsed time in milliseconds: 12

-------END-------

--------- Iterator Loop -------

Start: 1260903435648
End: 1260903435656
Iterator - Elapsed time in milliseconds: 8

-------END-------

--------- For Loop --------

Start: 1260903435656
End: 1260903435662
For - Elapsed time in milliseconds: 6

-------END-------

--------- While Loop -------

Start: 1260903435662
End: 1260903435668
While - Elapsed time in milliseconds: 6

Another test


--------- For Each Loop -------

Start: 1260903922225
End: 1260903922235
ForEach - Elapsed time in milliseconds: 10

-------END-------

--------- Iterator Loop -------

Start: 1260903922235
End: 1260903922244
Iterator - Elapsed time in milliseconds: 9

-------END-------

--------- For Loop --------

Start: 1260903922245
End: 1260903922251
For - Elapsed time in milliseconds: 6

-------END-------

--------- While Loop -------

Start: 1260903922251
End: 1260903922259
While - Elapsed time in milliseconds: 8

-------END-------


seems like foreach is slowest

tbenbrahim
15 Dec 2009, 10:54 PM
change the order of the loops :)