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返回的只能是纳秒级别的时间,缺乏灵活性。