java 中stopwatch,Stopwatch

/**

* An object that accurately measures elapsed time: the measured duration between two

* successive readings of "now" in the same process.

*一个精确计算消耗时间的对象:计算在同一进程中两次连续读取当前时间之间持续的时间

*

In contrast, wall time is a reading of "now" as given by a method like

* {

@link System#currentTimeMillis()}, best represented as an {

@link Instant}. Such values

* can be subtracted to obtain a {

@code Duration} (such as by {

@code Duration.between}), but

* doing so does not give a reliable measurement of elapsed time, because wall time readings

* are inherently approximate, routinely affected by periodic clock corrections. Because this class

* (by default) uses {

@link System#nanoTime}, it is unaffected by these changes.

*作为对照,实际时间是读取当前时间,就像System.currentTimeMillis()方法,一瞬间的最好代表。这种值相减可以得到一段时间,但是这样得不到准确的消耗的时间,因为实际时间读取是本质上近似的,

经常受到周期性时钟校正的影响。因为这个类使用System.nanoTime方法,它不受这些变化的影响

*

Use this class instead of direct calls to {@link System#nanoTime} for two reasons:

*用这个类代替直接调用System.nanoTime有两个原因

*

*

The raw {@code long} values returned by {@code nanoTime} are meaningless and unsafe to use

* in any other way than how {@code Stopwatch} uses them.

任何地方使用nanoTime返回的这个原生的long类型的值相对于Stopwatch使用它相比,毫无意义而且不安全

*

An alternative source of nanosecond ticks can be substituted, for example for testing or

* performance reasons, without affecting most of your code.

一个可供选择的资源是时间片段可以被替换,例如为了测试或性能原因,不影响你的大部分代码

*

*

*

Basic usage:

*

*

{@code

* Stopwatch stopwatch = Stopwatch.createStarted();

* doSomething();

* stopwatch.stop(); // optional

*

* Duration duration = stopwatch.elapsed();

*

* log.info("time: " + stopwatch); // formatted string like "12.3 ms"

* }

*

*

The state-changing methods are not idempotent; it is an error to start or stop a stopwatch

* that is already in the desired state.

*这个状态更改的方法不是幂等的,开始或停止一个已经处于期望状态的stopwatch对象是一个错误。

*

When testing code that uses this class, use {@link #createUnstarted(Ticker)} or {@link

* #createStarted(Ticker)} to supply a fake or mock ticker. This allows you to simulate any valid

* behavior of the stopwatch.

*当测试代码使用这个类,使用createUnstarted(Ticker)或createStarted(Ticker)提供一个假的或模拟的ticker,这里允许你模拟任何stopwatch有效的行为

*

Note: This class is not thread-safe.

*这个类是非线程安全的。

*

Warning for Android users: a stopwatch with default behavior may not continue to keep

* time while the device is asleep. Instead, create one like this:

*安卓用户的警告:设备休眠的时候stopwatch默认可能不会继续计算。代替的创建一个类似这种的代码:

*

{@code

* Stopwatch.createStarted(

* new Ticker() {

* public long read() {

* return android.os.SystemClock.elapsedRealtimeNanos();

* }

* });

* }

*

* @author Kevin Bourrillion

* @since 10.0

*/

总结:stopwatch主要功能就是两次star和stop调用之间的时间,还可以多次累加、可以重置。非线程安全的。

增加ticker的作用是可以自己替换和实现时间的读取。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值