Class Clock

java.lang.Object
com.saperion.common.lang.measuring.Clock

public class Clock extends Object

Clock is a utility class for time measurements.

There are static methods that support elementary time measurement functionality. For more complex scenarios an instance of Clock can be used.

A Clock-instance can be started and stopped several times and all the measured intervals will be summarized to a total time. If one of the getTotalTime-methods is called on a running measurement, it will return the summarized value of all previous intervals plus the current time interval without stopping the current measurement.

A new Clock is automatically running its first measurement.

A Clock can be reset (and will be stopped thereby).

Be aware that the accuracy is undefined. The most precise available system timer is used, but its accuracy is different on every system. Usually the overhead of the measurement plus the inaccuracy are in the order of 1000 nanoseconds (or less). Thus measurements of 0.1 milliseconds or more are very precise (less than one percent measurement inaccuracy), but a measurement beneath 10000 nanoseconds is not very reliable. Especially adding many very small intervals in a loop can lead to phantom results that the evaluator is not aware of.

Author:
agz
  • Constructor Details

    • Clock

      public Clock()
      Creates a new Clock that is automatically running its first measurement.
  • Method Details

    • reset

      public Clock reset()
      Resets the Clock.

      The clock is stopped thereby.

      Returns:
      this Clock
    • start

      public void start()
      Starts a new measurement interval on this Clock.

      This Clock must not be running already.

    • stop

      public long stop()
      Stops the current measurement and returns the elapsed intervals time in nanoseconds.

      This Clock must be running when this method is called.

      Returns:
      elapsed time in nanoseconds
    • isRunning

      public final boolean isRunning()
      Returns whether this Clock is currently running.
      Returns:
      whether this Clock is currently running
    • getTotalTime

      public final long getTotalTime()
      Returns the summarized time of all measurements of this Clock in nanoseconds.

      If this Clock is currently running, the current time interval will be measured and added without stopping the measurement.

      Returns:
      the total time in nanoseconds
    • getTotalTimeAsMicroseconds

      public final double getTotalTimeAsMicroseconds()
      Returns the total time as getTotalTime() does in microseconds.
      Returns:
      the total time in microseconds
      See Also:
    • getTotalTimeAsMilliseconds

      public final double getTotalTimeAsMilliseconds()
      Returns the total time as getTotalTime() does in milliseconds.
      Returns:
      the total time in milliseconds
      See Also:
    • getTotalTimeAsSeconds

      public final double getTotalTimeAsSeconds()
      Returns the total time as getTotalTime() does in seconds.
      Returns:
      the total time in seconds
      See Also:
    • getTotalTimeAsMinutes

      public final double getTotalTimeAsMinutes()
      Returns the total time as getTotalTime() does in minutes.
      Returns:
      the total time in minutes
      See Also:
    • getTotalTimeAsHours

      public final double getTotalTimeAsHours()
      Returns the total time as getTotalTime() does in hours.
      Returns:
      the total time in hours
      See Also:
    • getCountIntervals

      public final int getCountIntervals()
      Returns the count of measurement intervals (including the currently running if existent).
      Returns:
      the count of measurement intervals
    • currentTime

      public static long currentTime()
      Returns a relative indicator of current time of the most precise available system timer, with undefined accuracy in nanoseconds.

      Behaves like System.nanoTime().

      Returns:
      current time indicator
      See Also:
    • currentTimeInterval

      public static long currentTimeInterval(long startTime)
      Returns the elapsed time interval between the current time and the specified time indicator (measured by currentTime()) in nanoseconds.
      Parameters:
      startTime - start time indicator, measured by currentTime()
      Returns:
      time interval between the current time and the specified start time
      See Also:
    • currentTimeIntervalAsMilliseconds

      public static long currentTimeIntervalAsMilliseconds(long startTime)
      Returns the elapsed time interval between the current time and the specified time indicator (measured by currentTime()) in milliseconds truncated after the decimal point.
      Parameters:
      startTime - start time indicator, measured by currentTime(). Note that the start time is given in nanoseconds.
      Returns:
      time interval between the current time and the specified start time in milliseconds truncated after the decimal point
      See Also:
    • toString

      public String toString()
      Overrides:
      toString in class Object