Package com.gengoai

Class Stopwatch

  • All Implemented Interfaces:
    Serializable, AutoCloseable

    public class Stopwatch
    extends Object
    implements Serializable, AutoCloseable

    Tracks start and ending times to determine total time taken.

    Normal usage of a Stopwatch is a follows:

     
        var sw = Stopwatch.createStarted() //optionally you can give your Stopwatch a name
        //Perform some activity
        sw.stop();
        System.out.println(sw); // output total time
        sw.reset();
        sw.start();
        //Perform another activity
        sw.stop();
        System.out.println(sw); // output total time
     
     

    In cases where you do not want the stopwatch to start on creation you can use createStopped(). Additionally, you can use the Stopwatch as a resource to automatically log the timing on close as follows:

     
        //By default the log level is set to OFF and the Logger is the global logger
        //We change that by:
        try( var sw = Stopwatch.createStarted(MyLogger, Level.INFO) ){
           //Perform some activity
        }
     
     
    Author:
    David B. Bracewell
    See Also:
    Serialized Form
    • Method Detail

      • timeIt

        public static Stopwatch timeIt​(Runnable runnable)
        Calculates the time to execute the given runnable
        Parameters:
        runnable - the runnable to time
        Returns:
        the stopwatch in a stopped state
      • timeIt

        public static Stopwatch timeIt​(int nTrials,
                                       Runnable runnable)
        Calculates the time to execute the given runnable nTrial times
        Parameters:
        nTrials - the nunber of times to execute the runnable
        runnable - the runnable to time
        Returns:
        the stopwatch in a stopped state
      • createStarted

        public static Stopwatch createStarted​(String name)
        Create a named stopwatch that is started.
        Parameters:
        name - the name of the stopwatch for reporting purposes
        Returns:
        the stopwatch
      • createStarted

        public static Stopwatch createStarted​(String name,
                                              Logger logger,
                                              Level level)
        Create a stopwatch that is started.
        Parameters:
        name - the name of the stopwatch for reporting purposes
        Returns:
        the stopwatch
      • createStarted

        public static Stopwatch createStarted()
        Create a stopwatch that is started.
        Returns:
        the stopwatch
      • createStarted

        public static Stopwatch createStarted​(Logger logger,
                                              Level level)
        Create a stopwatch that is started.
        Returns:
        the stopwatch
      • createStopped

        public static Stopwatch createStopped​(String name)
        Create a stopwatch that is stopped.
        Parameters:
        name - the name of the stopwatch for reporting purposes
        Returns:
        the stopwatch
      • createStopped

        public static Stopwatch createStopped()
        Create a stopwatch that is stopped.
        Returns:
        the stopwatch
      • averageTime

        public Stopwatch averageTime​(long trials)
        Sets the elapsed time of the stopwatch to elapsed time / trials
        Parameters:
        trials - the number of trials to use to average the time
        Returns:
        this stopwatch
      • elapsed

        public long elapsed​(TimeUnit timeUnit)
        Gets the elapsed time in given time units
        Parameters:
        timeUnit - the time unit
        Returns:
        the elapsed time in the given time unit
      • getElapsedTime

        public long getElapsedTime()
        Gets elapsed time in nano seconds
        Returns:
        the elapsed time in nano seconds
      • reset

        public void reset()
        Reset the stopwatch.
      • resetAndStart

        public void resetAndStart()
      • start

        public void start()
        Start the stopwatch.
      • stop

        public void stop()
        Stop the stopwatch.