如何找到运行Java程序所需的时间?

如何找到运行Java程序所需的时间?,java,eclipse,runtime,Java,Eclipse,Runtime,我有一个我一直在开发的Java应用程序,我刚刚意识到程序必须在不到一分钟的时间内返回一个值,但不知道如何查找或显示运行程序所花费的时间。如何查找运行程序所用的时间?您可以使用System.nanoTime()比较时间。它将以纳秒为单位返回时间 返回最精确的可用系统计时器的当前值(以纳秒为单位) 您可以这样使用它: long startTime = System.nanoTime(); // code long endTime = System.nanoTime(); System.out.p

我有一个我一直在开发的Java应用程序,我刚刚意识到程序必须在不到一分钟的时间内返回一个值,但不知道如何查找或显示运行程序所花费的时间。如何查找运行程序所用的时间?

您可以使用
System.nanoTime()
比较时间。它将以纳秒为单位返回时间

返回最精确的可用系统计时器的当前值(以纳秒为单位)

您可以这样使用它:

long startTime = System.nanoTime();

// code

long endTime = System.nanoTime();
System.out.println("Took "+(endTime - startTime) + " ns"); 
有用链接:


    • 没有内置的方法可以查看程序运行了多长时间。但是,您可以在程序开始时只存储当前时间,以便稍后可以看到经过了多少时间

      public class MyProgram {
          private static long startTime = System.currentTimeMillis();
      
          public static void main(String[] args) {
              // Do stuff...
      
              // At the end
              long endTime = System.currentTimeMillis();
              System.out.println("It took " + (endTime - startTime) + " milliseconds");
          }
      }
      

      如果您只想知道程序运行多长时间,请在程序的开头和结尾使用System.currentTimeMillis()

      在您的应用程序上下文中:

      <aop:aspectj-autoproxy/>
      <bean id="myAspect" class="org.xyz.TimerAspect"/>   
      

      如果您的项目依赖于Spring(或者不介意添加它),您可以使用
      秒表。顾名思义,一旦初始化,它将计算时间,直到您停止它。然后,您可以检查任务所用的时间。可以同时使用多个秒表执行多个任务,以保持代码整洁

      除了保持代码整洁外,秒表还有助于格式化时间和其他实用方法

      public void myMethod(){
          StopWatch stopWatch = new StopWatch();
      
          stopWatch.start("Task1");
          // ...
          // Do my thing
          // ...
          stopWatch.stop();
          System.out.println("Task executed in " + stopWatch.getTotalTimeSeconds() + "s");
      }
      

      您可以使用
      System
      class提供的2个API

    • 如果代码在毫秒范围内花费时间
    • 如果代码在纳秒范围内花费时间
    • 示例代码

      public class TestTimeTaken {
          public static void main(String args[]) throws InterruptedException{
              long startTimeNanoSecond = System.nanoTime();
              long startTimeMilliSecond = System.currentTimeMillis();
      
              //code
              Thread.sleep(1000);
              //code
      
              long endTimeNanoSecond = System.nanoTime();
              long endTimeMilliSecond = System.currentTimeMillis();
      
              System.out.println("Time Taken in "+(endTimeNanoSecond - startTimeNanoSecond) + " ns");
              System.out.println("Time Taken in "+(endTimeMilliSecond - startTimeMilliSecond) + " ms");
      
      
          }
      }
      

      当我有可用的库时,我喜欢使用ApacheCommonsStopWatch类

      import org.apache.commons.lang3.time.StopWatch;
      
      // ...
      
      StopWatch stopWatch = new StopWatch();
      String message = "Task : %s (%s) seconds";
      
      // ...
      
      stopWatch.split();
      System.out.println(String.format(message, "10", stopWatch.toSplitString()));
      
      // ...
      
      stopWatch.split();
      System.out.println(String.format(message, "20", stopWatch.toSplitString()));
      
      stopWatch.stop();
      
      

      对不起,你的问题不够清楚。我认为日食在这里是无关紧要的。运行时也是不相关的,因为它令人困惑。JDK有类Runtime,您可以通过调用Runtime.getRuntime()获得它的单例实例,但它似乎不是您想要的。请试着重新思考你的问题并重新写。
      import org.apache.commons.lang3.time.StopWatch;
      
      // ...
      
      StopWatch stopWatch = new StopWatch();
      String message = "Task : %s (%s) seconds";
      
      // ...
      
      stopWatch.split();
      System.out.println(String.format(message, "10", stopWatch.toSplitString()));
      
      // ...
      
      stopWatch.split();
      System.out.println(String.format(message, "20", stopWatch.toSplitString()));
      
      stopWatch.stop();