告别System.currentTimeMillis()!Java高精度计时最佳实践

关注我的公众号:【编程朝花夕拾】,可获取首发内容。

01 引言

开过过程中,经常会遇到一些耗时的方法。当我们需要定位耗时的方法时,就需要逐步打印耗时已确认耗时的方法。或者在写单元测试的时候,粗略的估算方法耗时,是不是经常使用System.currentTimeMillis()去计算的。

今天,我们一起来整理一下可以使用那些方法计算耗时。

02 耗时计算方案

我们先罗列出常用的计算耗时的方法。我们需要模拟一个方法的耗时:

java 复制代码
private void invokeMethod() {
    try {
        System.out.println(DateUtil.now() + " invokeMethod 方法执行中......");
        // 模拟方法耗时:0~3000 ms
        Thread.sleep(new Random().nextLong(3000));
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    }
}

2.1 System.currentTimeMillis()

System.currentTimeMillis()毫秒级计算。

java 复制代码
@Test
void test01() {
    long start = System.currentTimeMillis();
    invokeMethod();
    long end = System.currentTimeMillis();
    System.out.println(DateUtil.now() + " 执行完毕,耗时:" + (end - start) + "ms");
    
    // 结果
    /*
     * 2025-07-11 14:19:55 invokeMethod 方法执行中......
     * 2025-07-11 14:19:58 执行完毕,耗时:2421ms
     */
}

2.2 System.nanoTime()

System.nanoTime() 纳秒级计算,计算方式和 System.currentTimeMillis()一致。

java 复制代码
@Test
void test02() {
    long start = System.nanoTime();
    invokeMethod();
    long end = System.nanoTime();
    System.out.println(DateUtil.now() + " 执行完毕,耗时:" + (end - start) + "ns");
}

2.3 Java8 InstantDuration

java 复制代码
@Test
void test03() {
    Instant start = Instant.now();
    invokeMethod();
    Instant end = Instant.now();
    System.out.println(DateUtil.now() + " 执行完毕,耗时:" + Duration.between(start, end).toMillis() + "ms");
}

2.4 Spring框架StopWatch

java 复制代码
@Test
void test04() {
    StopWatch stopWatch = new StopWatch();
    stopWatch.start();
    invokeMethod();
    stopWatch.stop();
    System.out.println(DateUtil.now() + " 执行完毕,耗时:" + stopWatch.getTotalTimeMillis() + "ms");
}

2.5 ApacheStopWatch

java 复制代码
@Test
void test05() {
    org.apache.commons.lang3.time.StopWatch stopWatch = new org.apache.commons.lang3.time.StopWatch();
    stopWatch.start();
    invokeMethod();
    stopWatch.stop();
    System.out.println(DateUtil.now() + " 执行完毕,耗时:" + stopWatch.getTime() + "ms");
}

2.6 hutoolStopWatch

java 复制代码
@Test
void test06() {
    cn.hutool.core.date.StopWatch stopWatch = new cn.hutool.core.date.StopWatch();
    stopWatch.start();
    invokeMethod();
    stopWatch.stop();
    System.out.println(DateUtil.now() + " 执行完毕,耗时:" + stopWatch.getTotalTimeMillis() + "ms");
}

2.7 GoogleStopwatch

这里要注意的是GoogleStopwatch中的watch为小写。

java 复制代码
@Test
void test06() {
    Stopwatch stopWatch = Stopwatch.createUnstarted();
    stopWatch.start();
    invokeMethod();
    stopWatch.stop();
    System.out.println(DateUtil.now() + " 执行完毕,耗时:" + stopWatch.elapsed().toMillis() + "ms");
}

2.8 阿里的Arthas

Arthas作为一款在线的Java诊断工具,被用作生产环境。和其他计算工具不同的是,这种方式没有代码的侵入性,启动Arthas后执行命令后,等待方法执行即可。

方法

java 复制代码
@RestController
public class TimeController {

    @RequestMapping("/test")
    public void test() {
        try {
            System.out.println(DateUtil.now() + " invokeMethod 方法执行中......");
            Thread.sleep(new Random().nextLong(3000));
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}

启动Arthas

选择当前运行的项目编号:

输入命令

sh 复制代码
trace com.simonking.boot.tool.controller.TimeController test  -n 5 --skipJDKMethod false 

运行方法

03 技术选型

耗时的计算用作测试,任意选择都无所谓,只要能达到目的即可。

  • 2.12.3用作单任务的耗时计算
  • 2.42.7用于复杂 的分段计算,无需不断的创建时间点,只需要重新调用start()方法即可,个别框架apache调用之前需要reset()重置之后方可使用。
  • 2.8用于生产问题的定位,可以观察每一段代码的耗时

04 小结

通过合理选择计时方法,可以更准确地获取代码性能数据,帮助优化关键路径逻辑。

相关推荐
兔兔爱学习兔兔爱学习5 小时前
Spring Al学习7:ImageModel
java·学习·spring
lang201509286 小时前
Spring远程调用与Web服务全解析
java·前端·spring
m0_564264186 小时前
IDEA DEBUG调试时如何获取 MyBatis-Plus 动态拼接的 SQL?
java·数据库·spring boot·sql·mybatis·debug·mybatis-plus
崎岖Qiu7 小时前
【设计模式笔记06】:单一职责原则
java·笔记·设计模式·单一职责原则
mapbar_front7 小时前
从大厂到中小公司,活下去的五个生存法则
程序员
Hello.Reader7 小时前
Flink ExecutionConfig 实战并行度、序列化、对象重用与全局参数
java·大数据·flink
熊小猿8 小时前
在 Spring Boot 项目中使用分页插件的两种常见方式
java·spring boot·后端
paopaokaka_luck8 小时前
基于SpringBoot+Vue的助农扶贫平台(AI问答、WebSocket实时聊天、快递物流API、协同过滤算法、Echarts图形化分析、分享链接到微博)
java·vue.js·spring boot·后端·websocket·spring
老华带你飞8 小时前
机器人信息|基于Springboot的机器人门户展示系统设计与实现(源码+数据库+文档)
java·数据库·spring boot·机器人·论文·毕设·机器人门户展示系统
notion20258 小时前
Adobe Lightroom Classic下载与安装教程(附安装包) 2025最新版详细图文安装教程
java·数据库·其他·adobe