StopWatch如何快速使用?

StopWatch如何快速使用?

import org.springframework.util.StopWatch;
public class StopWatchExample {
    public static void main(String[] args) {
    	//创建stopWatch对象
        StopWatch stopWatch = new StopWatch();
        // 开始计时 代码片段 起名task1
        stopWatch.start("task1");
        List<Integer> asList = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
        Integer sum = asList.stream().reduce(0, Integer::sum);
        System.out.println("sum = " + sum);
        // 结束计时  task1
        stopWatch.stop();
        // 开始计时 代码片段 起名task2
        stopWatch.start("task2");
        Long collect = asList.stream().collect(Collectors.counting());
        System.out.println("collect = " + collect);
        // 结束计时  task2
        stopWatch.stop();
        
        // 开始计时 代码片段 起名task3
        stopWatch.start("task3");
        Double avg = asList.stream().collect(Collectors.averagingDouble(Integer::doubleValue));
        System.out.println("avg = " + avg);
        // 结束计时  task3
        stopWatch.stop();
        // 开始计时 代码片段 起名task4
        stopWatch.start("task4");
        Integer sumInt = asList.stream().mapToInt(Integer::intValue).sum();
        // 结束计时  task4
        stopWatch.stop();
        // 开始计时 代码片段 起名task5
        stopWatch.start("task5");
        Integer max = asList.stream().max(Comparator.comparing(Integer::intValue)).get();
        Integer min = asList.stream().min(Comparator.comparing(Integer::intValue)).get();
       // 结束计时  task5
        stopWatch.stop();
        //打印日志  
        log.info("\n\uD83C\uDE2F⏩⏩⏩⏩⏩⏩\uD83D\uDD1C\uD83D\uDCB1打印结果\nhandleByPage -> {}执行完毕,耗时(秒) = {}, 详情 = {}",
                "简单测试", stopWatch.getTotalTimeSeconds(),
                stopWatch.prettyPrint());
    }
}

日志打印结果
在这里插入图片描述

stopwatch分析
1.spring自带工具类,可直接使用;
2.代码实现简单,使用更简单;
3.统一归纳,展示每项任务耗时与占用总时间的百分比,展示结果直观,性能消耗相对较小,并且最大程度的保证了start与stop之间的时间记录的准确性;
4.可在start时直接指定任务名字,从而更加直观的显示记录结果。
5.一个StopWatch实例一次只能开启一个task,不能同时start多个task,并且在该task未stop之前不能,start一个新的task,必须在该task stop之后才能开启新的task。
6.代码侵入式使用,需要改动多处代码。

缺点:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值