简体   繁体   English

计算方法的经过时间

[英]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. computation应该在方法调用beforeafter It should be endTime-startTime . 它应该是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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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