如何在 Java 中测量经过的时间?

一般来说,经过的时间是事件的起点到终点的时间。以下是在 Java 中查找经过时间的各种方法 -

使用currentTimeMillis()方法

该currentTimeMillis()方法以毫秒为单位返回当前时间。要查找方法的经过时间,您可以获取执行所需方法之前和之后的时间值之间的差异。

示例

public class Example {
   public void test(){
int num = 0;
      for(int i=0; i<=50; i++){  
         num =num+i;
         System.out.print(num+", ");
      }  
   }
   public static void main(String args[]){  
      //开始时间
      long begin = System.currentTimeMillis();
      //启动手表
      new Example().test();
      //时间结束
      long end = System.currentTimeMillis();
      long time = end-begin;
      System.out.println();
      System.out.println("经过时间: "+time +" milli seconds");
   }
}
输出结果
0, 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 66, 78, 91, 105, 120, 136, 153, 171, 190, 210, 231, 253, 276, 300, 325, 351, 378, 406, 435, 465, 496, 528, 561, 595, 630, 666, 703, 741, 780, 820, 861, 903, 946, 990, 1035, 1081, 1128, 1176, 1225, 1275,
经过时间: 4 milli seconds

使用nanoTime()方法

该nanoTime()方法以纳秒为单位返回当前时间。要查找方法的经过时间,您可以获取执行所需方法之前和之后的时间值之间的差异。

示例

public class Example {
   public void test(){
      int num = 0;
      for(int i=0; i<=50; i++){  
         num =num+i;
         System.out.print(num+", ");
      }  
   }
   public static void main(String args[]){  
      //开始时间
      long begin = System.nanoTime();
      //启动手表
      new Example().test();
      //时间结束
      long end = System.nanoTime();
      long time = end-begin;
      System.out.println();
      System.out.println("经过时间: "+time);
   }
}
输出结果
0, 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 66, 78, 91, 105, 120, 136, 153, 171, 190, 210, 231, 253, 276, 300, 325, 351, 378, 406, 435, 465, 496, 528, 561, 595, 630, 666, 703, 741, 780, 820, 861, 903, 946, 990, 1035, 1081, 1128, 1176, 1225, 1275,
经过时间: 1530200

使用即时类

now()Instant 类的方法返回当前时间,这些方法返回给定的两个时间值之间的差以获取经过的时间,检索执行所需方法之前和之后的时间值,并使用该方法检索持续时间。Duration.between()Duration.between()

示例

import java.time.Duration;
import java.time.Instant;
public class Example {
   public void test(){
      int num = 0;
      for(int i=0; i<=50; i++){  
         num =num+i;
         System.out.print(num+", ");
      }  
   }
   public static void main(String args[]) {  
      //起始时间
      Instant start = Instant.now();
      new Example().test();
      //时间结束
      Instant end = Instant.now();
      long time = Duration.between(start, end).toMillis();
      System.out.println();
      System.out.println(time+" Milli seconds");

   }
}
输出结果
0, 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 66, 78, 91, 105, 120, 136, 153, 171, 190, 210, 231, 253, 276, 300, 325, 351, 378, 406, 435, 465, 496, 528, 561, 595, 630, 666, 703, 741, 780, 820, 861, 903, 946, 990, 1035, 1081, 1128, 1176, 1225, 1275,
3 Milli seconds

使用秒表类

Apache commons 库提供了一个称为 Stopwatch 的类,它提供了start() stop()和getTime()方法来查找执行方法所花费的时间。

以下是这个包的 maven 文件 -

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.7</version>
</dependency>

示例

import org.apache.commons.lang3.time.StopWatch;
public class Example {
   public void test(){
      int num = 0;
      for(int i=0; i<=50; i++){  
         num =num+i;
         System.out.print(num+", ");
      }  
   }
   public static void main(String args[]) {  
      //实例化 StopWatch 类
      StopWatch obj = new StopWatch();
      //启动手表
      obj.start();
      new Example().test();
      //停止手表
      obj.stop();
      System.out.println();
      System.out.println("经过时间: "+obj.getTime() +" milli seconds");
   }
}
输出结果
0, 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 66, 78, 91, 105, 120, 136, 153, 171, 190, 210, 231, 253, 276, 300, 325, 351, 378, 406, 435, 465, 496, 528, 561, 595, 630, 666, 703, 741, 780, 820, 861, 903, 946, 990, 1035, 1081, 1128, 1176, 1225, 1275,
经过时间: 1 milli seconds