Loading

Spring StopWatch:优雅的统计代码块运行时间

StopWatch是Spring提供的一个工具类,可以方便的对同步单线程代码块计时,使用简单,写出的代码也比用System.currentTimeMillis()更加简洁。

参考资料:https://blog.csdn.net/gxs1688/article/details/87185030

Demo:

import org.springframework.util.StopWatch;

public class Demo {

    public static void main(String[] args) throws InterruptedException {
        StopWatch sw = new StopWatch("test");
        // 第一轮
        sw.start("任务1");
        Thread.sleep(100);
        sw.stop();
        // 第二轮
        sw.start("任务2");
        Thread.sleep(200);
        sw.stop();
        // 第三轮
        sw.start("任务1");
        Thread.sleep(100);
        sw.stop();
        System.out.println(sw.prettyPrint());
    }
}

输出结果:

StopWatch 'test': running time = 443757400 ns
---------------------------------------------
ns         %     Task name
---------------------------------------------
115454100  026%  任务1
215013000  048%  任务2
113290300  026%  任务1
posted @ 2020-10-28 15:30  n031  阅读(417)  评论(0编辑  收藏  举报