In the below code, iam calculating the time taken to calculate the Square Root of first 30000000 numbers. But there is a weird thing happening:
import java.util.*;
public class Cache
{
public static void main(String args[])
{
for(int j=0;j<=1000;j++)
{
;
}
float[] a = new float[30000000];
long as = System.currentTimeMillis();
for(int i=0;i<30000000;i++)
{
a[i]=(float) Math.sqrt(i);
}
long as2= System.currentTimeMillis();
System.out.println(as2-as);
}
}
Now in the for loop with counter j which is absolutely doing nothing, i found out that if i limit the counter till 1000 as shown above, the answer on an average is about 1500(ie msec). But if i increase the counter of j to 1000000. THe answer is 5000+.
This is weird because the time taken should be independent of how many times the loop is running.