「Java知識點分享」優雅的統計程序的執行時間,別再用System.currentTimeMillis()了
每天分享一點小知識,積少成多。今天分享的是如何優化的統計程序的執行時間,搞起!
1.不建議的方式
long begin = System.currentTimeMillis();
// 耗時的程序執行
long end = System.currentTimeMillis();
long costTime = begin - end;
2.優雅的方式(推薦)
// 不要在意這個代碼片段喲,這樣寫是為了簡便
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核心包
<!-- spring核心包,版本號根據自己的來喲,如果項目就是springboot項目是不需要單獨引入了的 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.0.2.RELEASE</version>
</dependency>
如果文章中涉及的內容有侵權行為請通知鄙人處理。

浙公網安備 33010602011771號