Class Timeit
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
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
Class representing a named checkpoint with a timestamp in nanoseconds. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionCreate a new checkpoint with a default name.checkpoint
(String name) Create a new checkpoint with the specified name.checkpointAndRun
(Runnable runnable) Create a checkpoint with the default name and immediately run the provided runnable.checkpointAndRun
(String name, Runnable runnable) Create a checkpoint and immediately run the provided Runnable.checkpointAndRun
(String name, Runnable runnable, int nIterations) Create a checkpoint and immediately run the provided Runnable nIterations times.Get an list of all the checkpoints.int
Get the maximum number of decimal places when reporting timings.static void
maxDecimalPlaces
(int maxDP) Set the maximum number of decimal places when reporting timings using seconds or minutes.Report timings in microseconds.Report timings in milliseconds.minutes()
Report timings in minutes.Report timings in nanoseconds.seconds()
Report timings in seconds.start()
Start the Timeit and create a checkpoint with the default name.Start the Timeit and create a checkpoint with the specified name.stop()
Stop theTimeit
.Request that checkpoints are summarized in thetoString()
method.summarizeCheckpoints
(boolean summarize) Optionally request that checkpoints are summarized in thetoString()
method.toString()
Returns a snapshot string representation of the Timeit's status.
-
Constructor Details
-
Timeit
public Timeit()
-
-
Method Details
-
start
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
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
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
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 createrunnable
- 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 > 1runnable
- the runnable to runnIterations
- the number of times to run the runnable- Returns:
- this instance
- Throws:
UnsupportedOperationException
- if the Timeit has already been stopped
-
checkpoint
Create a new checkpoint with a default name.- Returns:
- this instance
- Throws:
UnsupportedOperationException
- if the Timeit has already been stopped
-
checkpoint
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
Stop theTimeit
.- Returns:
- this instance
- Throws:
UnsupportedOperationException
- if the Timeit hasn't been started, or has already been stopped.
-
autoUnits
-
nanoseconds
Report timings in nanoseconds.- Returns:
- this instance
-
milliseconds
Report timings in milliseconds.- Returns:
- this instance
-
microseconds
Report timings in microseconds.- Returns:
- this instance
-
seconds
Report timings in seconds.- Returns:
- this instance
-
minutes
Report timings in minutes.- Returns:
- this instance
-
summarizeCheckpoints
Request that checkpoints are summarized in thetoString()
method. Currently, this means simply reporting the mean time per checkpoint.- Returns:
- this instance
- See Also:
-
summarizeCheckpoints
Optionally request that checkpoints are summarized in thetoString()
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
Set the maximum number of decimal places when reporting timings using seconds or minutes.- Parameters:
maxDP
-- Returns:
- this instance
-
getCheckpoints
Get an list of all the checkpoints.- Returns:
-
toString
Returns a snapshot string representation of the Timeit's status. -
main
-