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

    Constructors
    Constructor
    Description
    Constructs a new, unnamed timer.
    Timer(String name)
    Constructs a new timer with the given name.
  • Method Summary

    Modifier and Type
    Method
    Description
    TabularData<org.apache.commons.math3.stat.descriptive.StatisticalSummary>
    Returns the contents of this object as a TabularData instance, which can be used to save, print, or format the data in various ways.
    void
    Clears all timing data and resets the timer if it was previously started.
    double
    Returns the total elapsed time across all start / stop cycles.
    Returns the name given to this timer, or null if the timer is unnamed.
    org.apache.commons.math3.stat.descriptive.StatisticalSummary
    Returns the accumulated timing statistics for the timer.
    void
    Starts this timer.
    static Timer
    Creates a new, unnamed timer and starts it.
    static Timer
    Creates a new timer and starts it.
    double
    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

    Methods inherited from interface org.moeaframework.util.format.Formattable

    display, display, display, save, saveCSV
  • Constructor Details

    • Timer

      public Timer()
      Constructs a new, unnamed timer.
    • Timer

      public Timer(String name)
      Constructs a new timer with the given name.
      Parameters:
      name - the timer name
  • Method Details

    • startNew

      public static Timer startNew()
      Creates a new, unnamed timer and starts it. This is the equivalent of creating a new timer and calling start().
      Returns:
      the new timer
    • startNew

      public static Timer startNew(String name)
      Creates a new timer and starts it. This is the equivalent of creating a new timer and calling start().
      Parameters:
      name - the timer name
      Returns:
      the new timer
    • getName

      public String getName()
      Returns the name given to this timer, or null 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

      public TabularData<org.apache.commons.math3.stat.descriptive.StatisticalSummary> asTabularData()
      Description copied from interface: Formattable
      Returns the contents of this object as a TabularData instance, which can be used to save, print, or format the data in various ways.
      Specified by:
      asTabularData in interface Formattable<org.apache.commons.math3.stat.descriptive.StatisticalSummary>
      Returns:
      the TabularData instance