guava Stopwatch

guava提供了Stopwatch用于计算程序执行中的时间消耗。那么为什么需要这个类了?我们首先看看通常我们是如何计算时间消耗的。

        long start = System.nanoTime();
        Uninterruptibles.sleepUninterruptibly(2, TimeUnit.SECONDS);
        System.out.println("耗时:" + (System.nanoTime() - start));

如果使用Stopwatch应该如何写这段逻辑呢?代码如下:

        Stopwatch stopwatch = Stopwatch.createStarted();
        Uninterruptibles.sleepUninterruptibly(2, TimeUnit.SECONDS);
        System.out.println("耗时:" + stopwatch.elapsed(TimeUnit.NANOSECONDS));

通过上述代码,可以看到Stopwatch将计时的逻辑封装在自己内部,通过暴露start和elapsed等api供使用方去调用,并且提供方便的时间单位转换能力。对于我来说,Stopwatch将计时的操作和时间单位的描述显示的描述出来,使代码更直观。

通过查看源码上的注释,可以看到该类的作者记录的Stopwatch创建的意图:

 * <ul>
 * <li>An alternate time source can be substituted, for testing or performance
 *     reasons.
 * <li>As documented by {@code nanoTime}, the value returned has no absolute
 *     meaning, and can only be interpreted as relative to another timestamp
 *     returned by {@code nanoTime} at a different time. {@code Stopwatch} is a
 *     more effective abstraction because it exposes only these relative values,
 *     not the absolute ones.
 * </ul>

列举了两个原因:

  • 可替换的时间源,Stopwatch可以在创建的时候指定对应的时间。示例:
        Stopwatch stopwatch = Stopwatch.createStarted(new Ticker() {
            @Override
            public long read() {
                return System.nanoTime();
            }
        });
  • Stopwatch返回的时间是可以指定具体格式。而nanoTime返回的只能是纳秒级别的时间,缺乏灵活性。
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值