如何优雅的打印代码执行时长

如何优雅的打印代码执行时长

System方式 不推荐

  public static void main(String[] args) {
        long startTime = System.currentTimeMillis(); 
        
        //...

        long endTime = System.currentTimeMillis(); 
        System.out.println("程序运行时间: "   (endTime - startTime)   "ms");
    }

JDK8 处理方式 不推荐

 public static void main(String[] args) {
        Instant start = Instant.now();
        
        //...
        
        Instant end = Instant.now();

        Duration duration = Duration.between(start, end);
        
        System.out.println("millis = "   duration.toMillis());
         System.out.println("程序运行时间: "   duration.toMillis()   "ms");
    }

Commons-lang3的StopWatch 推荐方式

  public static void main(String[] args) throws Exception {
        StopWatch watch = StopWatch.createStarted(); //创建后立即start,常用
        //StopWatch watch = new StopWatch();
        //watch.start();

        Thread.sleep(1000);
        System.out.println("统计从开始到现在运行时间:"   watch.getTime()   "ms"); //1000ms

        Thread.sleep(1000);
        watch.split();
        System.out.println("从start到此刻为止的时间:"   watch.getTime());
        System.out.println("从开始到第一个切入点运行时间:"   watch.getSplitTime()); //2245

        Thread.sleep(1000);
        watch.split();
        System.out.println("从开始到第二个切入点运行时间:"   watch.getSplitTime());

        watch.reset(); //重置后必须使用start方法
        watch.start();
        Thread.sleep(1000);
        System.out.println("重新开始后到当前运行时间是:"   watch.getTime()); //1000

        watch.suspend(); //暂停
        Thread.sleep(6000); //模拟暂停6秒钟

        watch.resume(); //上面suspend,这里要想重新统计,需要恢复一下
        System.out.println("恢复后执行的时间是:"   watch.getTime()); //1000  注意此时这个值还是1000

        watch.stop();
        System.out.println("花费的时间》》"   watch.getTime()   "ms"); //1002ms
        System.out.println("花费的时间》》"   watch.getTime(TimeUnit.SECONDS)   "s"); //1s 可以直接转成s

    }

打印结果

统计从开始到现在运行时间:1007ms
从start到此刻为止的时间:2008
从开始到第一个切入点运行时间:2008
从开始到第二个切入点运行时间:3009
重新开始后到当前运行时间是:1000
恢复后执行的时间是:1000
花费的时间》》1001ms
花费的时间》》1s

Spring的StopWatch 推荐方式

 public static void main(String[] args) throws Exception {
        // 强烈每一个秒表都给一个id,这样查看日志起来能够更加的精确
        // 至于Id 我觉得给UUID是可行的~
        StopWatch sw = new StopWatch(UUID.randomUUID().toString());

        sw.start("起床");
        Thread.sleep(1000);
        System.out.println("当前任务名称:"   sw.currentTaskName());
        sw.stop();

        sw.start("洗漱");
        Thread.sleep(2000);
        System.out.println("当前任务名称:"   sw.currentTaskName());
        sw.stop();

        sw.start("锁门");
        Thread.sleep(500);
        System.out.println("当前任务名称:"   sw.currentTaskName());
        sw.stop();

        System.out.println(sw.prettyPrint()); // 这个方法打印在我们记录日志时是非常友好的  还有百分比的分析哦
        System.out.println(sw.shortSummary());
        System.out.println(sw.currentTaskName()); // stop后它的值为null


        // 最后一个任务的相关信息
        System.out.println(sw.getLastTaskName());
        System.out.println(sw.getLastTaskInfo());

        // 任务总的耗时  如果你想获取到每个任务详情(包括它的任务名、耗时等等)可使用
        System.out.println("所有任务总耗时:"   sw.getTotalTimeMillis());
        System.out.println("任务总数:"   sw.getTaskCount());
        System.out.println("所有任务详情:"   sw.getTaskInfo()); // 拿到所有的任务
    }

当前任务名称:起床
当前任务名称:洗漱
当前任务名称:锁门
StopWatch 'd6ba9412-d551-4ba7-8b0e-1b7ccb42855d': running time (millis) = 3504
-----------------------------------------
ms     %     Task name
-----------------------------------------
01001  029%  起床
02000  057%  洗漱
00503  014%  锁门

StopWatch 'd6ba9412-d551-4ba7-8b0e-1b7ccb42855d': running time (millis) = 3504
null
锁门
org.springframework.util.StopWatch$TaskInfo@2d554825
所有任务总耗时:3504
任务总数:3
所有任务详情:[Lorg.springframework.util.StopWatch$TaskInfo;@68837a77
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章