java stopwatch 使用

public class Program {

    public static void main(String[] args) throws InterruptedException {
        var sw = new StopWatch("模板测试");

        sw.start("任务1");
        Thread.sleep(1000 * 1);
        sw.stop();

        sw.start("任务2");
        Thread.sleep(1000 * 2);
        sw.stop();

        sw.start("任务3");
        Thread.sleep(1000 * 3);
        sw.stop();

        //打印各子任务耗时比例
        System.out.println(sw.prettyPrint());
        System.out.println("所有任务总耗时:" + sw.getTotalTimeMillis() + "ms");
    }
}

输出如下:

StopWatch '模板测试': running time = 6030843500 ns
---------------------------------------------
ns         %     Task name
---------------------------------------------
1013406000  017%  任务1
2004607200  033%  任务2
3012830300  050%  任务3

所有任务总耗时:6030ms

 

posted on 2022-08-18 14:36  空明流光  阅读(1098)  评论(1编辑  收藏  举报

导航