用StopWatch 统计代码耗时

2023-12-25 19:31:06 浏览数 (1)

统计输出总耗时

代码语言:javascript复制
StopWatch sw = new StopWatch();
      sw.start();
      // long task simulation
      try {
          Thread.sleep(1000);
      } catch (Exception e) {
          e.printStackTrace();
      }
      sw.stop();
      System.out.println(sw.getTotalTimeMillis());

以优雅的格式打出所有任务的耗时以及占比

代码语言:javascript复制
 StopWatch sw = new StopWatch();
      sw.start("A");
      Thread.sleep(500);
      sw.stop();
      sw.start("B");
      Thread.sleep(300);
      sw.stop();
      sw.start("C");
      Thread.sleep(200);
      sw.stop();
      System.out.println(sw.prettyPrint());

参考

https://mp.weixin.qq.com/s/RNsuFaONmruEnyJGKad5sA

0 人点赞