Java統(tǒng)計方法代碼執(zhí)行時間

在日常代碼開發(fā)中,經(jīng)常會用到統(tǒng)計方法或代碼執(zhí)行時長,用于對業(yè)務(wù)代碼做優(yōu)化。常用的代碼統(tǒng)計時間方法有一下6種:


代碼統(tǒng)計方法

一、System.currentTimeMillis

Java 內(nèi)置的方法,使用 System.currentTimeMillis 來統(tǒng)計執(zhí)行的時間(統(tǒng)計單位:毫秒),示例代碼如下:

public class TimeIntervalTest {
    public static void main(String[] args) throws InterruptedException {
      // 開始時間
      long stime = System.currentTimeMillis();
      // 執(zhí)行時間(1s)
      Thread.sleep(1000);
      // 結(jié)束時間
      long etime = System.currentTimeMillis();
      // 計算執(zhí)行時間
      System.out.printf("執(zhí)行時長:%d 毫秒.", (etime - stime));
  }
}

執(zhí)行結(jié)果為:

執(zhí)行時長:1000 毫秒.

二、new Date

Java 的內(nèi)置方法,在開始執(zhí)行前 new Date() 創(chuàng)建一個當(dāng)前時間對象,在執(zhí)行結(jié)束之后 new Date() 一個當(dāng)前執(zhí)行時間,然后再統(tǒng)計兩個 Date 的時間間隔,示例代碼如下:

public class TimeIntervalTest {
  public static void main(String[] args) throws InterruptedException {
      // 開始時間
      Date sdate = new Date();
      // 執(zhí)行時間(1s)
      Thread.sleep(1000);
      // 結(jié)束時間
      Date edate = new Date();
      //  統(tǒng)計執(zhí)行時間(毫秒)
      System.out.printf("執(zhí)行時長:%d 毫秒." , (edate.getTime() - sdate.getTime())); 
  }
}

執(zhí)行結(jié)果為:

執(zhí)行時長:1000 毫秒.

三、System.nanoTime

Java 內(nèi)置的方法,使用 System.nanoTime 來統(tǒng)計執(zhí)行時間(統(tǒng)計單位:納秒),它的執(zhí)行方法和 System.currentTimeMillis 類似,示例代碼如下:

public class TimeIntervalTest {
  public static void main(String[] args) throws InterruptedException {
      // 開始時間
      long stime = System.nanoTime();
      // 執(zhí)行時間(1s)
      Thread.sleep(1000);
      // 結(jié)束時間
      long etime = System.nanoTime();
      // 計算執(zhí)行時間
      System.out.printf("執(zhí)行時長:%d 納秒.", (etime - stime));
  }
}

執(zhí)行結(jié)果為:

執(zhí)行時長:1000965670納秒.

1 毫秒 = 100 萬納秒。

四、Spring StopWatch

項目是 Spring 或 Spring Boot 項目,可以在項目中直接使用 StopWatch 對象來統(tǒng)計代碼執(zhí)行時間,示例代碼如下:

StopWatch stopWatch = new StopWatch();
// 開始時間
stopWatch.start();
// 執(zhí)行時間(1s)
Thread.sleep(1000);
//結(jié)束時間
stopwatch.stop();
// 清空計時器
stopwatch.reset();
 // 再次啟動時間
stopWatch.start();
// 執(zhí)行時間(1s)
Thread.sleep(1000);
// 結(jié)束時間
stopWatch.stop();
// 統(tǒng)計執(zhí)行時間(秒)
System.out.printf("執(zhí)行時長:%d 秒.%n", stopWatch.getTotalTimeSeconds()); // %n 為換行
// 統(tǒng)計執(zhí)行時間(毫秒)
System.out.printf("執(zhí)行時長:%d 毫秒.%n", stopWatch.getTotalTimeMillis()); 
// 統(tǒng)計執(zhí)行時間(納秒)
System.out.printf("執(zhí)行時長:%d 納秒.%n", stopWatch.getTotalTimeNanos());

執(zhí)行結(jié)果為:

執(zhí)行時長:0.9996313 秒. 執(zhí)行時長:999 毫秒. 執(zhí)行時長:999631300 納秒.

五、commons-lang3 StopWatch

如果是普通項目,也可以用 Apache commons-lang3 中的 StopWatch 對象來實現(xiàn)時間統(tǒng)計,首先先添加 commons-lang3 的依賴:

<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
  <groupId>org.apache.commons</groupId>
  <artifactId>commons-lang3</artifactId>
  <version>3.10</version>
</dependency>

統(tǒng)計代碼:

import org.apache.commons.lang3.time.StopWatch;
import java.util.concurrent.TimeUnit;

public class TimeIntervalTest {
  public static void main(String[] args) throws InterruptedException {
    StopWatch stopWatch = new StopWatch();
    // 開始時間
    stopWatch.start();
    // 執(zhí)行時間(1s)
    Thread.sleep(1000);
    // 結(jié)束時間
    stopWatch.stop();
    // 統(tǒng)計執(zhí)行時間(秒)
    System.out.println("執(zhí)行時長:" + stopWatch.getTime(TimeUnit.SECONDS) + " 秒.");
    // 統(tǒng)計執(zhí)行時間(毫秒)
    System.out.println("執(zhí)行時長:" + stopWatch.getTime(TimeUnit.MILLISECONDS) + " 毫秒.");
    // 統(tǒng)計執(zhí)行時間(納秒)
    System.out.println("執(zhí)行時長:" + stopWatch.getTime(TimeUnit.NANOSECONDS) + " 納秒.");
  }
}

執(zhí)行結(jié)果為:

執(zhí)行時長:1 秒. 執(zhí)行時長:1000 毫秒.
執(zhí)行時長:1000555100 納秒.

六、Guava Stopwatch

Google 的 Guava,Guava 中也包含了 Stopwatch 統(tǒng)計類。首先先添加 Guava 的依賴:

<!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
<dependency>
  <groupId>com.google.guava</groupId>
  <artifactId>guava</artifactId>
  <version>29.0-jre</version>
</dependency>

統(tǒng)計代碼:

import com.google.common.base.Stopwatch;
import java.util.concurrent.TimeUnit;

public class TimeIntervalTest {
  public static void main(String[] args) throws InterruptedException {
    // 創(chuàng)建并啟動計時器
    Stopwatch stopwatch = Stopwatch.createStarted();
    // 執(zhí)行時間(1s)
    Thread.sleep(1000);
    // 停止計時器
    stopwatch.stop();
    // 執(zhí)行時間(單位:秒)
    System.out.printf("執(zhí)行時長:%d 秒. %n", stopwatch.elapsed().getSeconds()); // %n 為換行
    // 執(zhí)行時間(單位:毫秒)
    System.out.printf("執(zhí)行時長:%d 豪秒.", stopwatch.elapsed(TimeUnit.MILLISECONDS));
  }
}

執(zhí)行結(jié)果為:

執(zhí)行時長:1 秒.
執(zhí)行時長:1000 豪秒.
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容