(转载)Java统计代码的执行时间的6种方法

(37条消息) Java统计代码的执行时间的6种方法_年少青山的博客-CSDN博客_java记录程序执行时间

img

方法一:System.currentTimeMillis

此方法为 Java 内置的方法,使用 System#currentTimeMillis 来统计执行的时间(统计单位:毫秒),示例代码如下:

image-20220310005835924

image-20220310005845181

方法二:System.nanoTime

此方法为 Java 内置的方法,使用 System#nanoTime 来统计执行时间(统计单位:纳秒),它的执行方法和 System#currentTimeMillis 类似,示例代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
public class TimeIntervalTest {
public static void main(String[] args) throws InterruptedException {
// 开始时间
long stime = System.nanoTime();
// 执行时间(1s)
Thread.sleep(1000);
// 结束时间
long etime = System.nanoTime();
// 计算执行时间
System.out.printf("执行时长:%d 纳秒.", (etime - stime));
}
}

执行时长:1000769200 纳秒.

小贴士:1 毫秒 = 100 万纳秒。

方法三:new Date

此方法也是 Java 的内置方法,在开始执行前 new Date() 创建一个当前时间对象,在执行结束之后 new Date() 一个当前执行时间,然后再统计两个 Date 的时间间隔,示例代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import java.util.Date;

public class TimeIntervalTest {
public static void main(String[] args) throws InterruptedException {
// 开始时间
Date sdate = new Date();
// 执行时间(1s)
Thread.sleep(1000);
// 结束时间
Date edate = new Date();
// 统计执行时间(毫秒)
System.out.printf("执行时长:%d 毫秒." , (edate.getTime() - sdate.getTime()));
}
}

执行时长:1000 毫秒

方法四:Spring StopWatch

如果我们使用的是 Spring 或 Spring Boot 项目,可以在项目中直接使用 StopWatch 对象来统计代码执行时间,示例代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
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());

执行时长:0.9996313 秒. 执行时长:999 毫秒. 执行时长:999631300 纳秒.

小贴士:Thread#sleep 方法的执行时间稍有偏差,在 1s 左右都是正常的。

方法五:commons-lang3 StopWatch

如果我们使用的是普通项目,那我们可以用 Apache commons-lang3 中的 StopWatch 对象来实现时间统计,首先先添加 commons-lang3 的依赖:

1
2
3
4
5
6
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.10</version>
</dependency>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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();
// 执行时间(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) + " 纳秒.");
}
}

执行时长:1 秒. 执行时长:1000 毫秒.

执行时长:1000555100 纳秒.

方法六:Guava Stopwatch

除了 Apache 的 commons-lang3 外,还有一个常用的 Java 工具包,那就是 Google 的 Guava,Guava 中也包含了 Stopwatch 统计类。首先先添加 Guava 的依赖:

1
2
3
4
5
6
<!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>29.0-jre</version>
</dependency>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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().getSeconds()); // %n 为换行
// 执行时间(单位:毫秒)
System.out.printf("执行时长:%d 豪秒.", stopwatch.elapsed(TimeUnit.MILLISECONDS));
}
}

执行时长:1 秒.

执行时长:1000 豪秒.

总结

本文介绍了 6 种实现代码统计的方法,其中 3 种是 Java 内置的方法:

System.currentTimeMillis()

System.nanoTime()

new Date()

还介绍了 3 种常用框架 spring、commons-langs3、guava 的时间统计器 StopWatch。

在没有用到 spring、commons-langs3、guava 任意一种框架的情况下,推荐使用 System.currentTimeMillis() 或 System.nanoTime() 来实现代码统计,否则建议直接使用 StopWatch 对象来统计执行时间。

知识扩展—Stopwatch 让统计更方便

StopWatch 存在的意义是让代码统计更简单,比如 Guava 中 StopWatch 使用示例如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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));
}
}