Calculating elapsed time of method

43 views Asked by At

I'm trying to record the elapsed time for my method in milliseconds. Could someone tell me what I'm doing wrong?

public static void main(String[] args)
{
     double pi = computePi(10000);

     System.out.println(pi);

     System.out.println(startTime - endTime);
}
  long startTime = System.currentTimeMillis();
public static double computePi(int count) 

{

    double pi = 0;

    for(int i = 0; i < count; i++)
    {
        pi += Math.pow(-1,i)/(2*i+1);
  long endTime = System.currentTimeMillis();
    }
    return pi * 4;
    return startTime - endTime;
}

}

1

There are 1 answers

0
BatScream On

The computation should be just before and after the method call. It should be endTime-startTime.

public static void main(String[] args)
{
     long startTime = System.currentTimeMillis();
     double pi = computePi(10000);
     long endTime = System.currentTimeMillis();

     System.out.println(pi);

     System.out.println(endTime- startTime);
}