Package org.moeaframework.util
Class Timer
java.lang.Object
org.moeaframework.util.Timer
- All Implemented Interfaces:
Named,Displayable,Formattable<org.apache.commons.math3.stat.descriptive.StatisticalSummary>
public class Timer
extends Object
implements Formattable<org.apache.commons.math3.stat.descriptive.StatisticalSummary>, Named
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
Constructors -
Method Summary
Modifier and TypeMethodDescriptionTabularData<org.apache.commons.math3.stat.descriptive.StatisticalSummary>Returns the contents of this object as aTabularDatainstance, which can be used to save, print, or format the data in various ways.voidclear()Clears all timing data and resets the timer if it was previously started.doubleReturns the total elapsed time across all start / stop cycles.getName()Returns the name given to this timer, ornullif the timer is unnamed.org.apache.commons.math3.stat.descriptive.StatisticalSummaryReturns the accumulated timing statistics for the timer.voidstart()Starts this timer.static TimerstartNew()Creates a new, unnamed timer and starts it.static TimerCreates a new timer and starts it.doublestop()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, waitMethods 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, ornullif the timer is unnamed. -
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:FormattableReturns the contents of this object as aTabularDatainstance, which can be used to save, print, or format the data in various ways.- Specified by:
asTabularDatain interfaceFormattable<org.apache.commons.math3.stat.descriptive.StatisticalSummary>- Returns:
- the
TabularDatainstance
-