https://blog.csdn.net/gxs1688/article/details/87185030
说明
spring-framework提供的一个StopWatch类可以做类似任务执行时间控制,也就是封装了一个对开始时间,结束时间记录操作的Java类。
示例
start开始记录,stop停止记录,然后通过StopWatch的prettyPrint方法,可直观的输出代码执行耗时,以及执行时间百分比。
public class TestStopWatch {
public static void main(String[] args) throws InterruptedException {
StopWatch sw = new StopWatch();
sw.start("doSomething1");
Thread.sleep(200);
sw.stop();
sw.start("doSomething2");
Thread.sleep(200);
sw.stop();
sw.start("doSomething3");
Thread.sleep(200);
sw.stop();
System.out.println(sw.prettyPrint());
}
}
控制台打印结果如下
StopWatch '': running time = 613210100 ns
---------------------------------------------
ns % Task name
---------------------------------------------
201980400 033% doSomething1
201809600 033% doSomething2
209420100 034% doSomething3
Process finished with exit code 0