Package com.helger.commons.timing
Class StopWatch
- java.lang.Object
-
- com.helger.commons.timing.StopWatch
-
- All Implemented Interfaces:
IStoppable
@NotThreadSafe public class StopWatch extends Object implements IStoppable
Simple stop watch based onSystem.nanoTime().- Author:
- Philip Helger
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedStopWatch(boolean bStart)Constructor.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static StopWatchcreatedStarted()static StopWatchcreatedStopped()protected static longgetCurrentNanoTime()DurationgetDuration()DurationgetLapDuration()Create an intermediate time (lap time).longgetMillis()longgetNanos()booleanisStarted()booleanisStopped()EChangereset()Reset all saved durations, in case this stop watch is to be used in a loop.voidrestart()Stops, resets and starts the stop watch.static DurationrunMeasured(Runnable aRunnable)Run the passed runnable and measure the time.EChangestart()Start the stop watch.EChangestop()Stop the stop watch.DurationstopAndGetDuration()Stop the stop watch and get the duration since the start.longstopAndGetMillis()Stop the stop watch and get the elapsed milliseconds since the start.StringtoString()
-
-
-
Method Detail
-
reset
@Nonnull public EChange reset()
Reset all saved durations, in case this stop watch is to be used in a loop. Does not change the start/stop state.- Returns:
EChange.
-
getCurrentNanoTime
protected static final long getCurrentNanoTime()
- Returns:
- The current time in nano seconds.
-
stop
@Nonnull public EChange stop()
Stop the stop watch.- Specified by:
stopin interfaceIStoppable- Returns:
EChange.CHANGEDif the stop watch was previously running and is now stopped, andEChange.UNCHANGEDif the stop watch was already stopped.
-
restart
public void restart()
Stops, resets and starts the stop watch.
-
isStarted
public boolean isStarted()
- Returns:
trueif the stop watch is currently started (running),falseotherwise.
-
isStopped
public boolean isStopped()
- Returns:
trueif the stop watch is currently stopped (not running),falseotherwise.
-
getNanos
@Nonnegative public long getNanos()
- Returns:
- The elapsed nano seconds (1000 nano seconds = 1 milli second).
-
getMillis
@Nonnegative public long getMillis()
- Returns:
- The elapsed milli seconds.
-
stopAndGetMillis
@Nonnegative public long stopAndGetMillis()
Stop the stop watch and get the elapsed milliseconds since the start. If the stop watch was started and stopped multiple times, the duration is added.- Returns:
- The elapsed milli seconds or 0 if the stop watch was never started.
-
stopAndGetDuration
@Nonnull public Duration stopAndGetDuration()
Stop the stop watch and get the duration since the start. If the stop watch was started and stopped multiple times, the duration is added.- Returns:
- The elapsed duration or 0 if the stop watch was never started.
-
getLapDuration
@Nonnull public Duration getLapDuration()
Create an intermediate time (lap time). It internally stops, takes the duration and restarts.- Returns:
- The elapsed duration or 0 if the stop watch was never started.
- Since:
- 10.0.0
-
createdStarted
@Nonnull @ReturnsMutableCopy public static StopWatch createdStarted()
- Returns:
- A new
StopWatchobject that is started. Nevernull.
-
createdStopped
@Nonnull @ReturnsMutableCopy public static StopWatch createdStopped()
- Returns:
- A new
StopWatchobject that is NOT started. Nevernull.
-
runMeasured
@Nonnull public static Duration runMeasured(@Nonnull Runnable aRunnable)
Run the passed runnable and measure the time. In v10 this was changed to return Duration instead of TimeValue.- Parameters:
aRunnable- The runnable to be executed. May not benull.- Returns:
- The elapsed Duration. Never
null.
-
-