Package org.moeaframework.util
Class Timer
java.lang.Object
org.moeaframework.util.Timer
- All Implemented Interfaces:
Displayable
,Formattable<org.apache.commons.math3.stat.descriptive.StatisticalSummary>
public class Timer
extends Object
implements Formattable<org.apache.commons.math3.stat.descriptive.StatisticalSummary>
Utility for measuring elapsed time. Unlike a "stopwatch" which measures the total elapsed time if started and
stopped multiple times, this timer is intended to measure timing statistics from repeated invocations. Every
start / stop cycle records the elapsed time from which statistics (min, mean, max, etc.) are computed.
While this timer internally uses System.nanoTime()
for up to nanosecond accuracy, the values reported are
converted into fractional seconds.
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionTabularData<org.apache.commons.math3.stat.descriptive.StatisticalSummary>
Returns the contents of this object as aTabularData
instance, which can be used to save, print, or format the data in various ways.void
clear()
Clears all timing data and resets the timer if it was previously started.double
Returns the total elapsed time across all start / stop cycles.getName()
Returns the name given to this timer, ornull
if the timer is unnamed.org.apache.commons.math3.stat.descriptive.StatisticalSummary
Returns the accumulated timing statistics for the timer.void
start()
Starts this timer.static Timer
startNew()
Creates a new, unnamed timer and starts it.static Timer
Creates a new timer and starts it.double
stop()
Stops the timer and records the elapsed time.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.moeaframework.util.format.Displayable
display
-
Constructor Details
-
Timer
public Timer()Constructs a new, unnamed timer. -
Timer
Constructs a new timer with the given name.- Parameters:
name
- the timer name
-
-
Method Details
-
startNew
Creates a new, unnamed timer and starts it. This is the equivalent of creating a new timer and callingstart()
.- Returns:
- the new timer
-
startNew
Creates a new timer and starts it. This is the equivalent of creating a new timer and callingstart()
.- Parameters:
name
- the timer name- Returns:
- the new timer
-
getName
Returns the name given to this timer, ornull
if the timer is unnamed.- Returns:
- the timer name
-
start
public void start()Starts this timer. -
stop
public double stop()Stops the timer and records the elapsed time.- Returns:
- the elapsed time, in seconds, since this timer was last started
-
getElapsedTime
public double getElapsedTime()Returns the total elapsed time across all start / stop cycles.- Returns:
- the total elapsed time, in seconds
-
getStatistics
public org.apache.commons.math3.stat.descriptive.StatisticalSummary getStatistics()Returns the accumulated timing statistics for the timer.- Returns:
- the accumulated timing statistics
-
clear
public void clear()Clears all timing data and resets the timer if it was previously started. -
asTabularData
Description copied from interface:Formattable
Returns the contents of this object as aTabularData
instance, which can be used to save, print, or format the data in various ways.- Specified by:
asTabularData
in interfaceFormattable<org.apache.commons.math3.stat.descriptive.StatisticalSummary>
- Returns:
- the
TabularData
instance
-