Java:计算代码耗时的5种方法
总阅读量:207次
摘要: 原创出处 blog.csdn.net/inrgihc 「inrgihc」欢迎转载,保留摘要,谢谢!
- 1、使用System.currentTimeMillis()函数
- 2、使用System.nanoTime()函数
- 3、在java8中使用Instant.now()函数
- 4、使用apache.commons提供的StopWatch
- 5、使用Spring 框架提供的StopWatch
在java开发过程中,常常会用一些方法来计算一段代码的耗时,那么java中计算耗时的方法有哪些,这里整理总结如下:
1、使用System.currentTimeMillis()函数
代码如下:
long start = System.currentTimeMillis(); |
2、使用System.nanoTime()函数
代码如下:
long start = System.nanoTime(); |
3、在java8中使用Instant.now()函数
代码如下:
Instant start = Instant.now(); |
4、使用apache.commons提供的StopWatch
首先,在pom.xml中添加如下依赖:
<dependency> |
5、使用Spring 框架提供的StopWatch
代码如下:
import org.springframework.util.StopWatch; |