0

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;
}

}

zoisi
  • 11
  • 1
  • 3
  • possible duplicate of [How do I time a method's execution in Java?](http://stackoverflow.com/questions/180158/how-do-i-time-a-methods-execution-in-java) – en_Knight Nov 15 '14 at 04:40

1 Answers1

0

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);
}
BatScream
  • 19,260
  • 4
  • 52
  • 68