Wednesday, September 28, 2011

Reverse loop versus Forward loop in Performance – Java


Forward Looping
Forward looping is loop from starting element and continues to the end of the elements. 0..1..2..3..End. Most of the time, may be i can said all the time we are using this type of looping method to loop through an array, List or collection.
for (int i=0; i< lList.size(); i++)
Reverse Looping
Reverse looping is loop from end of the element and continues to the starting elements. End..9..8..0. Most of the time, may be i can said all the time we are not using this type of looping method.
for (int i=lList.size()-1; i > 0; i--)
Performance Test
Here i create two tiny programs to use forward loop and reverse loop to traverse 15 millions of data, and display the elapse time in output.
1. Forward Looping
import java.util.Arrays;
import java.util.Date;
import java.util.List;

public class LoopTestForward {
  public static void main(String[] argv) {

          String sArray[] = createArray();

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

          //Forward Loop Testing
          System.out.println("\n--------- Forward Loop --------\n");
          long lForwardStartTime = new Date().getTime();
          System.out.println("Start: " + lForwardStartTime);

          //for loop
          for (int i=0; i< iListSize; i++){
                 String stemp = (String)lList.get(i);
          }

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

          long lForwardDifference = lForwardEndTime - lForwardStartTime;
          System.out.println("Forward Looping - Elapsed time in milliseconds: " + lForwardDifference);

          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;
  }
}
2. Reverse Looping
import java.util.Arrays;
import java.util.Date;
import java.util.List;

public class LoopTestReverse {
  public static void main(String[] argv) {

          String sArray[] = createArray();

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

          //Reverse Loop Testing
          System.out.println("\n--------- Reverse Loop --------\n");
          long lReverseStartTime = new Date().getTime();
          System.out.println("Start: " + lReverseStartTime);

          //for loop
          for (int i=iListSize-1; i > 0; i--){
                 String stemp = (String)lList.get(i);
          }

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

          long lReverseDifference = lReverseEndTime - lReverseStartTime;
          System.out.println("For - Elapsed time in milliseconds: " + lReverseDifference);

          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;
  }
}
D:\test>java -Xms1024m -Xmx1024m LoopTestFoward
D:\test>java -Xms1024m -Xmx1024m LoopTestReverse
Performance Test Result (in milliseconds)

Conclusion
The result show there’s not much different between forward and reverse looping in 1 million of data. However when data grow huge, the performance of reverse looping is slightly faster than forward looping around 15%.
May be you will question about who so stupid to load 1 million of data into a single List or Collection. Please imagine a multi-thread system environment, where 100k people concurrent access your “forward loop x 10″, it already over 1 million. Ya i know sometime the different is negligible, but i do believe believe “Reverse loop” is a good habit in programming , it can increase the performance. It’s sound weird …but performance show.

No comments: