Package adams.core
Class TimeIt
- java.lang.Object
-
- adams.core.TimeIt
-
- All Implemented Interfaces:
Serializable
public abstract class TimeIt extends Object implements Serializable
Helper class for timing code execution.
For example, testing the time it takes reading a CSV file:final String filename = "/some/file.csv"; new TimeIt("CSV: ") { protected void doRun() { CsvSpreadSheetReader reader = new CsvSpreadSheetReader(); reader.read(filename); } }.run();
Or using the static "timeIt" method with a Runnable:TimeIt.timeIt("CSV: ", () -> { CsvSpreadSheetReader reader = new CsvSpreadSheetReader(); reader.read(filename); });
Will output something like this:CSV: 00:00:25.929
- Author:
- FracPete (fracpete at waikato dot ac dot nz)
- See Also:
- Serialized Form
-
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected abstract void
doRun()
The actual code to run.protected void
output(String msg)
Outputs the message.long
run()
Times the execution and outputs the elapsed time.static long
timeIt(Runnable r)
Executes the runnable.static long
timeIt(String prefix, Runnable r)
Executes the runnable.
-
-
-
Field Detail
-
m_Prefix
protected String m_Prefix
the prefix for logging.
-
-
Constructor Detail
-
TimeIt
public TimeIt()
Initializes with no output on commandline.
-
TimeIt
public TimeIt(String prefix)
Initializes with the specified prefix for output.- Parameters:
prefix
- the prefix to use, use null to suppress output on commandline
-
-
Method Detail
-
output
protected void output(String msg)
Outputs the message.- Parameters:
msg
- the message to output
-
doRun
protected abstract void doRun()
The actual code to run.
-
run
public long run()
Times the execution and outputs the elapsed time.- Returns:
- the duration in msec
-
timeIt
public static long timeIt(Runnable r)
Executes the runnable. Does not output anything on commandline.- Parameters:
r
- the runnable- Returns:
- the duration in msec
-
-