计算方法的经过时间

时间:2014-11-15 04:37:43

标签: java time pi elapsedtime

我正在尝试以毫秒为单位记录我方法的已用时间。有人能告诉我我做错了吗?

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 个答案:

答案 0 :(得分:0)

computation应该只是beforeafter方法调用。它应该是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);
}