1、使用System.currentTimeMillis()函数ps:本文来自“Java 笔记虾” 公众号,本人转载
代码如下:
long start = System.currentTimeMillis(); // some code long finish = System.currentTimeMillis(); long timeElapsed = finish - start;2、使用System.nanoTime()函数
代码如下:
long start = System.nanoTime(); // some code long finish = System.nanoTime(); long timeElapsed = finish - start;3、在java8中使用Instant.now()函数
代码如下:
Instant start = Instant.now(); // some code Instant finish = Instant.now(); long timeElapsed = Duration.between(start, finish).toMillis();4、使用apache.commons提供的StopWatch
首先,在pom.xml中添加如下依赖:
5、使用Spring 框架提供的StopWatchorg.apache.commons commons-lang3 3.7
代码如下:
import org.springframework.util.StopWatch;
StopWatch watch = new StopWatch();
watch.start("watcher");
//some code
watch.stop();
System.out.println(watch.prettyPrint());



