When I run this code I got time like 0.25s, I want to improve this code to get time better than 0.16s - this is the limit. I know it depends on the hardware. Time is compute without filling array. I must work on array. How can I improve this ?
Update: I want to get the index of first element of slice which avg is minimal.
public static void main(String[] args) {
int[] A = new int[10000] ;
double randomNum ;
for(int i= 0 ; i<A.length ; i++)
{
randomNum = 0 + (double)(Math.random()*1);
if(randomNum>0.5)
A[i]=-1;
else
A[i]=1;
}
System.out.println("Compute");
long start_time = System.nanoTime();
int result = 0;
double minavg = 100000 ;
double avg;
double suma = 0;
int count = 0;
for(int i=0 ; i<A.length-1 ; i++)
{
for(int k=i ; k<A.length ; k++)
{
suma+=A[k];
count++;
if(count>1)
{
if(A[k]<A[k-1]) {
avg=suma/count;
if(minavg>avg)
{
minavg=avg;
result = i;
}
}
}
}
suma=0;
count=0;
}
long end_time = System.nanoTime();
double difference = (end_time - start_time)/1e9;
System.out.println(difference);
}