Package adams.core

Class 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
    • 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
      • timeIt

        public static long timeIt​(String prefix,
                                  Runnable r)
        Executes the runnable.
        Parameters:
        prefix - the prefix to use for output, use null to suppress output on commandline
        r - the runnable
        Returns:
        the duration in msec