简体   繁体   中英

Calculating elapsed time of method

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

}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM