Package com.helger.base.timing
Class StopWatch
java.lang.Object
com.helger.base.timing.StopWatch
- All Implemented Interfaces:
IStoppable
Simple stop watch based on
System.nanoTime().- Author:
- Philip Helger
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic @NonNull StopWatchstatic @NonNull StopWatchprotected static final long@NonNull Duration@NonNull DurationCreate an intermediate time (lap time).longlonggetNanos()booleanboolean@NonNull 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 @NonNull DurationrunMeasured(@NonNull Runnable aRunnable) Run the passed runnable and measure the time.final @NonNull EChangestart()Start the stop watch.@NonNull EChangestop()Stop the stop watch.@NonNull DurationStop the stop watch and get the duration since the start.longStop the stop watch and get the elapsed milliseconds since the start.toString()
-
Constructor Details
-
StopWatch
protected StopWatch(boolean bStart) Constructor.- Parameters:
bStart- iftruethe stop watch is directly started!
-
-
Method Details
-
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.
-
start
Start the stop watch.- Returns:
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.- See Also:
-
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.
-
getDuration
- Returns:
- The elapsed duration. Never
null.
-
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
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
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
-
toString
-
createdStarted
- Returns:
- A new
StopWatchobject that is started. Nevernull.
-
createdStopped
- Returns:
- A new
StopWatchobject that is NOT started. Nevernull.
-
runMeasured
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.
-