如何在 Java 中計算經過時間/執行時間?


通常,經過時間或執行時間是從事件的起點到終點的時長。以下是幾種在 Java 中查詢經過時間的方法:

  • currentTimeMillis() 方法以毫秒為單位返回當前時間。要查詢某個方法的經過時間,您可以獲取目標方法執行前後時間值的差值。
  • nanoTime() 方法以納秒為單位返回當前時間。要查詢某個方法的經過時間,您可以獲取目標方法執行前後時間值的差值。
  • Instant 類的 now() 方法返回當前時間,Duration.between() 方法返回兩個給定時間值之間的差值,您可以使用它們來查詢方法執行所花費的時間。
  • Apache commons 庫提供了一個名為 Stopwatch 的類,它提供 start()、stop() 和 getTime() 方法,您可以使用它們來查詢方法執行所花費的時間。
  • java.util.Date 類的 getTime() 方法返回當前時間。要查詢某個方法的經過時間,您可以獲取目標方法執行前後時間值的差值。
  • 您還可以使用**getInstance().getTime().getTime()**獲取當前例項中的時間,然後透過獲取差值來查詢經過時間。

示例:使用 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[]){  
      //Start time
      long begin = System.currentTimeMillis();
      //Starting the watch
      new Example().test();
      //End time
      long end = System.currentTimeMillis();      
     
      long time = end-begin;
      System.out.println();
      System.out.println("Elapsed Time: "+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,
Elapsed Time: 4 milli seconds

示例:使用 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[]){  
      //Start time
      long begin = System.nanoTime();
      //Starting the watch
      new Example().test();
      //End time
      long end = System.nanoTime();      
     
      long time = end-begin;
      System.out.println();
      System.out.println("Elapsed Time: "+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,
Elapsed Time: 1530200

示例:使用 Instant 類

線上演示

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[]) {  
      //Starting time
      Instant start = Instant.now();
      new Example().test();
      //End time
      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

示例:使用 StopWatch 類

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[]) {  
      //Instantiating the StopWatch class
      StopWatch obj = new StopWatch();
      //Starting the watch
      obj.start();
      new Example().test();
      //Stopping the watch
      obj.stop();
      System.out.println();
      System.out.println("Elapsed Time: "+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,
Elapsed Time: 1 milli seconds

示例:使用 getTime() 方法

線上演示

import java.util.Date;
public class Demo{
   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 startTime = new Date().getTime();
       new Demo().test();
      long endTime = new Date().getTime();
      long time = endTime - startTime;
      System.out.println("Execution time: " + 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, Execution time: 5

示例:使用 Calendar 類

線上演示

import java.util.Calendar;
public class Demo{
   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) throws InterruptedException {
      long startTime = Calendar.getInstance().getTime().getTime();
       new Demo().test();
      long endTime = Calendar.getInstance().getTime().getTime();
      long time = endTime - startTime;
      System.out.println("Execution time: " + 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, Execution time: 20

更新於:2021年2月6日

7K+ 瀏覽量

開啟您的職業生涯

透過完成課程獲得認證

開始學習
廣告