Class Timeit

java.lang.Object
qupath.lib.common.Timeit

public class Timeit extends Object
Helper class to time code execution. It is designed to be particularly useful when scripting. The main timing report is implemented as timeit.toString() so that it can easily be printed.

A simple use is to start the timer before running code and print it at the end:


 var timeit = new Timeit().start();
 // Do something
 timeit.stop(); // Optional - we can also print at any time
 System.out.println(timeit);
 
 

Checkpoints can also be added to output times for individual stages:


 var timeit = new Timeit();
 timeit.checkpoint("First checkpoint");
 // Do something
 timeit.checkpoint("Second checkpoint");
 // Do something else
 timeit.stop();
 System.out.println(timeit);
 
 

Finally, a Timeit can be used to repeatedly run the same code multiple times, and print the timings at the end.


 var timeit = new Timeit()
   .microseconds()
   .checkpointAndRun("Greeting", () -> System.out.println("Hello!"), 10)
   .summarizeCheckpoints()
   .stop();
 System.out.println(timeit);
 
 
Since:
v0.4.0
Author:
Pete Bankhead
  • Constructor Details

    • Timeit

      public Timeit()
  • Method Details

    • start

      public Timeit start() throws UnsupportedOperationException
      Start the Timeit and create a checkpoint with the default name.

      Note that calling this method is not essential, because the Timeit will automatically be started when the first checkpoint is created.

      Returns:
      this instance
      Throws:
      UnsupportedOperationException - if the Timeit has previously been started
      See Also:
    • start

      public Timeit start(String name) throws UnsupportedOperationException
      Start the Timeit and create a checkpoint with the specified name.

      Note that calling this method is not essential, because the Timeit will automatically be started when the first checkpoint is created.

      Parameters:
      name - of the checkpoint; if null, a default name will be generated
      Returns:
      this instance
      Throws:
      UnsupportedOperationException - if the Timeit has previously been started
      See Also:
    • checkpointAndRun

      public Timeit checkpointAndRun(Runnable runnable) throws UnsupportedOperationException
      Create a checkpoint with the default name and immediately run the provided runnable.

      Note that no checkpoint is made automatically after completion, so you should generally print the output immediately, create a new checkpoint, or call stop().

      Parameters:
      runnable - the runnable to run
      Returns:
      this instance
      Throws:
      UnsupportedOperationException - if the Timeit has already been stopped
    • checkpointAndRun

      public Timeit checkpointAndRun(String name, Runnable runnable) throws UnsupportedOperationException
      Create a checkpoint and immediately run the provided Runnable.

      Note that no checkpoint is made automatically after completion, so you should generally print the output immediately, create a new checkpoint, or call stop().

      Parameters:
      name - the name of the checkpoint to create
      runnable - the runnable to run
      Returns:
      this instance
      Throws:
      UnsupportedOperationException - if the Timeit has already been stopped
    • checkpointAndRun

      public Timeit checkpointAndRun(String name, Runnable runnable, int nIterations) throws UnsupportedOperationException
      Create a checkpoint and immediately run the provided Runnable nIterations times. The purpose of this is to get better insights into the time required to run a self-contained block of code.

      Note that no checkpoint is made automatically after completion of the final iteration, so you should generally print the output immediately, create a new checkpoint, or call stop().

      Parameters:
      name - base name of the checkpoint to create; the iteration number will be appended if nIterations > 1
      runnable - the runnable to run
      nIterations - the number of times to run the runnable
      Returns:
      this instance
      Throws:
      UnsupportedOperationException - if the Timeit has already been stopped
    • checkpoint

      public Timeit checkpoint() throws UnsupportedOperationException
      Create a new checkpoint with a default name.
      Returns:
      this instance
      Throws:
      UnsupportedOperationException - if the Timeit has already been stopped
    • checkpoint

      public Timeit checkpoint(String name) throws UnsupportedOperationException
      Create a new checkpoint with the specified name.
      Parameters:
      name - name of the checkpoint; if null, a default name will be generated
      Returns:
      this instance
      Throws:
      UnsupportedOperationException - if the Timeit has already been stopped
    • stop

      public Timeit stop() throws UnsupportedOperationException
      Stop the Timeit.
      Returns:
      this instance
      Throws:
      UnsupportedOperationException - if the Timeit hasn't been started, or has already been stopped.
    • autoUnits

      public Timeit autoUnits()
    • nanoseconds

      public Timeit nanoseconds()
      Report timings in nanoseconds.
      Returns:
      this instance
    • milliseconds

      public Timeit milliseconds()
      Report timings in milliseconds.
      Returns:
      this instance
    • microseconds

      public Timeit microseconds()
      Report timings in microseconds.
      Returns:
      this instance
    • seconds

      public Timeit seconds()
      Report timings in seconds.
      Returns:
      this instance
    • minutes

      public Timeit minutes()
      Report timings in minutes.
      Returns:
      this instance
    • summarizeCheckpoints

      public Timeit summarizeCheckpoints()
      Request that checkpoints are summarized in the toString() method. Currently, this means simply reporting the mean time per checkpoint.
      Returns:
      this instance
      See Also:
    • summarizeCheckpoints

      public Timeit summarizeCheckpoints(boolean summarize)
      Optionally request that checkpoints are summarized in the toString() method. Currently, this means simply reporting the mean time per checkpoint.
      Parameters:
      summarize - whether to summarize or not
      Returns:
      this instance
      See Also:
    • getMaxDecimalPlaces

      public int getMaxDecimalPlaces()
      Get the maximum number of decimal places when reporting timings.
      Returns:
      See Also:
    • maxDecimalPlaces

      public Timeit maxDecimalPlaces(int maxDP)
      Set the maximum number of decimal places when reporting timings using seconds or minutes.
      Parameters:
      maxDP -
      Returns:
      this instance
    • getCheckpoints

      public List<Timeit.Checkpoint> getCheckpoints()
      Get an list of all the checkpoints.
      Returns:
    • toString

      public String toString()
      Returns a snapshot string representation of the Timeit's status.
      Overrides:
      toString in class Object
    • main

      public static void main(String[] args)