开发需要测试一些代码执行时间,本文汇总常用方法。

# System.currentTimeMillis

Java 内置方法

// 开始时间
long stime = System.currentTimeMillis();
// 执行时间(1s)
Thread.sleep(1000);
// 结束时间
long etime = System.currentTimeMillis();
// 统计执行时间
System.out.printf("执行时长:%d 毫秒", (etime - stime));

# System.nanoTime

Java 内置方法(统计单位:纳秒)

// 开始时间
long stime = System.nanoTime();
// 执行时间(1s)
Thread.sleep(1000);
// 结束时间
long etime = System.nanoTime();
// 统计执行时间
System.out.printf("执行时长:%d 纳秒", (etime - stime));

# new Date

Java 内置方法

// 开始时间
Date sdate = new Date();
// 执行时间(1s)
Thread.sleep(1000);
// 结束时间
Date edate = new Date();
// 统计执行时间(毫秒)
System.out.printf("执行时长:%d 毫秒", (edate.getTime() - sdate.getTime()));

# Spring StopWatch

添加 Spring 或 Spring Boot 依赖,StopWatch 实现时间统计

StopWatch stopWatch = new StopWatch();
// 开始时间
stopWatch.start();
// 执行时间(1s)
Thread.sleep(1000);
// 结束时间
stopWatch.stop();
// 统计执行时间(秒)
System.out.printf("执行时长:%d 秒%n", stopWatch.getTotalTimeSeconds()); // % n 为换行
// 统计执行时间(毫秒)
System.out.printf("执行时长:%d 毫秒%n", stopWatch.getTotalTimeMillis());
// 统计执行时间(纳秒)
System.out.printf("执行时长:%d 纳秒%n", stopWatch.getTotalTimeNanos());

# commons-lang3 StopWatch

添加 Apache 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>

StopWatch 实现时间统计

StopWatch stopWatch = new StopWatch();
// 开始时间
stopWatch.start();
// 执行时间(1s)
Thread.sleep(1000);
// 结束时间
stopWatch.stop();
// 统计执行时间(秒)
System.out.println("执行时长:" + stopWatch.getTime(TimeUnit.SECONDS) + " 秒");
// 统计执行时间(毫秒)
System.out.println("执行时长:" + stopWatch.getTime(TimeUnit.MILLISECONDS) + " 毫秒");
// 统计执行时间(纳秒)
System.out.println("执行时长:" + stopWatch.getTime(TimeUnit.NANOSECONDS) + " 纳秒");

# Guava Stopwatch

添加 Google Guava 依赖

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

StopWatch 实现时间统计

// 创建并启动计时器
Stopwatch stopwatch = Stopwatch.createStarted();
// 执行时间(1s)
Thread.sleep(1000);
// 停止计时器
stopwatch.stop();
// 统计执行时间(单位:秒)
System.out.printf("执行时长:%d 秒 %n", stopwatch.elapsed().getSeconds()); // % n 为换行
// 统计执行时间(单位:毫秒)
System.out.printf("执行时长:%d 毫秒", stopwatch.elapsed(TimeUnit.MILLISECONDS));

可以使用一个 Stopwatch 对象统计多段代码执行时间,也可以指定时间单位。

import com.google.common.base.Stopwatch;
import java.util.concurrent.TimeUnit;
public class TimeIntervalTest {
    public static void main(String[] args) throws InterruptedException {
        // 创建并启动计时器
        Stopwatch stopwatch = Stopwatch.createStarted();
        // 执行时间(1s)
        Thread.sleep(1000);
        // 停止计时器
        stopwatch.stop();
        // 执行统计
        System.out.printf("执行时长:%d 毫秒 %n",
                stopwatch.elapsed(TimeUnit.MILLISECONDS));
        // 清空计时器
        stopwatch.reset();
        // 再次启动统计
        stopwatch.start();
        // 执行时间(2s)
        Thread.sleep(2000);
        // 停止计时器
        stopwatch.stop();
        // 执行统计
        System.out.printf("执行时长:%d 秒 %n",
                stopwatch.elapsed(TimeUnit.MILLISECONDS));
    }
}
更新于