Class Clock
- java.lang.Object
-
- com.saperion.common.lang.measuring.Clock
-
public class Clock extends java.lang.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 thegetTotalTime
-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
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static long
currentTime()
Returns a relative indicator of current time of the most precise available system timer, with undefined accuracy in nanoseconds.static long
currentTimeInterval(long startTime)
Returns the elapsed time interval between the current time and the specified time indicator (measured bycurrentTime()
) in nanoseconds.static long
currentTimeIntervalAsMilliseconds(long startTime)
Returns the elapsed time interval between the current time and the specified time indicator (measured bycurrentTime()
) in milliseconds truncated after the decimal point.int
getCountIntervals()
Returns the count of measurement intervals (including the currently running if existent).long
getTotalTime()
Returns the summarized time of all measurements of thisClock
in nanoseconds.double
getTotalTimeAsHours()
Returns the total time asgetTotalTime()
does in hours.double
getTotalTimeAsMicroseconds()
Returns the total time asgetTotalTime()
does in microseconds.double
getTotalTimeAsMilliseconds()
Returns the total time asgetTotalTime()
does in milliseconds.double
getTotalTimeAsMinutes()
Returns the total time asgetTotalTime()
does in minutes.double
getTotalTimeAsSeconds()
Returns the total time asgetTotalTime()
does in seconds.boolean
isRunning()
Returns whether thisClock
is currently running.Clock
reset()
Resets theClock
.void
start()
Starts a new measurement interval on thisClock
.long
stop()
Stops the current measurement and returns the elapsed intervals time in nanoseconds.java.lang.String
toString()
-
-
-
Constructor Detail
-
Clock
public Clock()
Creates a newClock
that is automatically running its first measurement.
-
-
Method Detail
-
start
public void start()
-
stop
public long stop()
Stops the current measurement and returns the elapsed intervals time in nanoseconds. ThisClock
must be running when this method is called.- Returns:
- elapsed time in nanoseconds
-
isRunning
public final boolean isRunning()
Returns whether thisClock
is currently running.- Returns:
- whether this
Clock
is currently running
-
getTotalTime
public final long getTotalTime()
Returns the summarized time of all measurements of thisClock
in nanoseconds. If thisClock
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 asgetTotalTime()
does in microseconds.- Returns:
- the total time in microseconds
- See Also:
getTotalTime()
-
getTotalTimeAsMilliseconds
public final double getTotalTimeAsMilliseconds()
Returns the total time asgetTotalTime()
does in milliseconds.- Returns:
- the total time in milliseconds
- See Also:
getTotalTime()
-
getTotalTimeAsSeconds
public final double getTotalTimeAsSeconds()
Returns the total time asgetTotalTime()
does in seconds.- Returns:
- the total time in seconds
- See Also:
getTotalTime()
-
getTotalTimeAsMinutes
public final double getTotalTimeAsMinutes()
Returns the total time asgetTotalTime()
does in minutes.- Returns:
- the total time in minutes
- See Also:
getTotalTime()
-
getTotalTimeAsHours
public final double getTotalTimeAsHours()
Returns the total time asgetTotalTime()
does in hours.- Returns:
- the total time in hours
- See Also:
getTotalTime()
-
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 likeSystem.nanoTime()
.- Returns:
- current time indicator
- See Also:
System.nanoTime()
-
currentTimeInterval
public static long currentTimeInterval(long startTime)
Returns the elapsed time interval between the current time and the specified time indicator (measured bycurrentTime()
) in nanoseconds.- Parameters:
startTime
- start time indicator, measured bycurrentTime()
- Returns:
- time interval between the current time and the specified start time
- See Also:
currentTime()
-
currentTimeIntervalAsMilliseconds
public static long currentTimeIntervalAsMilliseconds(long startTime)
Returns the elapsed time interval between the current time and the specified time indicator (measured bycurrentTime()
) in milliseconds truncated after the decimal point.- Parameters:
startTime
- start time indicator, measured bycurrentTime()
. 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:
currentTimeInterval(long)
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
-