I have two methods which does the same operation, but with a slight difference in for loop..
METHOD I
--------
public void loopcontrol()
{
String str = "abcdefghijklmnopqurstuvwxyz";
long st = System.currentTimeMillis();
System.out.println("Starting Time = "+st);
int len = str.length();
for (int j = 0; j < len; j++) {
System.out.println(j);
}
long endt = System.currentTimeMillis();
System.out.println("Finishing Time = "+endt);
System.out.println("Total No of MilliSeconds to perform the opertaion = "+(endt-st));
}
METHOD II
---------
public void secondLoopcontrol()
{
String str = "abcdefghijklmnopqurstuvwxyz";
long st = System.currentTimeMillis();
System.out.println("Starting Time = "+st);
int len = str.length();
for (int j = len -1 ; j >= 0; j--) {//for loop alone changed
System.out.println(j);
}
long endt = System.currentTimeMillis();
System.out.println("Finishing Time = "+endt);
System.out.println("Total No of MilliSeconds to perform thre opertaion = "+(endt-st));
}
When i execute it using main method, the first method took 110 milliseconds while the second method took just 30 milliseconds.
Can anyone explain what can be the possible reason behind this ???