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 on System.nanoTime().
Author:
Philip Helger
  • Constructor Details

    • StopWatch

      protected StopWatch(boolean bStart)
      Constructor.
      Parameters:
      bStart - if true the stop watch is directly started!
  • Method Details

    • 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.
    • start

      @Nonnull public final EChange start()
      Start the stop watch.
      Returns:
      EChange.
    • stop

      @Nonnull public EChange stop()
      Stop the stop watch.
      Specified by:
      stop in interface IStoppable
      Returns:
      EChange.CHANGED if the stop watch was previously running and is now stopped, and EChange.UNCHANGED if the stop watch was already stopped.
    • restart

      public void restart()
      Stops, resets and starts the stop watch.
      See Also:
    • isStarted

      public boolean isStarted()
      Returns:
      true if the stop watch is currently started (running), false otherwise.
    • isStopped

      public boolean isStopped()
      Returns:
      true if the stop watch is currently stopped (not running), false otherwise.
    • 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

      @Nonnull public Duration 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

      @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
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • createdStarted

      @Nonnull @ReturnsMutableCopy public static StopWatch createdStarted()
      Returns:
      A new StopWatch object that is started. Never null.
    • createdStopped

      @Nonnull @ReturnsMutableCopy public static StopWatch createdStopped()
      Returns:
      A new StopWatch object that is NOT started. Never null.
    • 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 be null.
      Returns:
      The elapsed Duration. Never null.