Java
数据库
开源项目
电子书
合作推广
首页
Java
优雅的统计程序的执行时间,别再用System.currentTimeMillis()了
原创
分类:
Java
作者:
Java老牛
发布日期:
2022-10-11T22:40:53
每天分享一点小知识,积少成多。今天分享的是如何优化的统计程序的执行时间,搞起! ## 1.不建议的方式 ```java long begin = System.currentTimeMillis(); // 耗时的程序执行 long end = System.currentTimeMillis(); long costTime = begin - end; ``` ## 2.优雅的方式(推荐) ```java // 不要在意这个代码片段哟,这样写是为了简便 import org.springframework.util.StopWatch; StopWatch stopWatch = new StopWatch(); stopWatch.start(); // 耗时的程序执行 stopWatch.stop(); System.out.println("cost time: " + stopWatch.getTotalTimeMillis() + " ms"); System.out.println("cost time: " + stopWatch.getTotalTimeSeconds() + " s"); ``` ### 2.1需要引入spring核心包 ```xml
org.springframework
spring-core
4.0.2.RELEASE
```
目录