public final class GuavaStopwatch extends Object
System.nanoTime() for a few reasons:
nanoTime, the value returned has no absolute meaning, and can only
be interpreted as relative to another timestamp returned by nanoTime at a different
time. Stopwatch is a more effective abstraction because it exposes only these
relative values, not the absolute ones.
Basic usage:
Stopwatch stopwatch = Stopwatch.createStarted();
doSomething();
stopwatch.stop(); // optional
Duration duration = stopwatch.elapsed();
log.info("time: " + stopwatch); // formatted string like "12.3 ms"
Stopwatch methods are not idempotent; it is an error to start or stop a stopwatch that is already in the desired state.
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.createStarted(
new Ticker() {
public long read() {
return android.os.SystemClock.elapsedRealtimeNanos();
}
});
| Modifier and Type | Method and Description |
|---|---|
String |
toString()
Returns a string representation of the current elapsed time.
|
Copyright © 2019. All rights reserved.