Class Timing


  • public class Timing
    extends Object
    A class for measuring how long things take. For backward compatibility, this class contains static methods, but the preferred usage is to instantiate a Timing object and use instance methods.

    To use, call startTime() before running the code in question. Call tick() to print an intermediate update, and endTime() to finish the timing and print the result. You can optionally pass a descriptive string and PrintStream to tick and endTime for more control over what gets printed where.

    Example: time reading in a big file and transforming it:

    Timing.startTime();
    String bigFileContents = IOUtils.slurpFile(bigFile);
    Timing.tick("read in big file", System.err);
    String output = costlyTransform(bigFileContents);
    Timing.endTime("transformed big file", System.err);

    Author:
    Bill MacCartney
    • Constructor Detail

      • Timing

        public Timing()
        Constructs new Timing object and starts the timer.
    • Method Detail

      • start

        public void start()
        Start timer.
      • report

        public long report()
        Return elapsed time (without stopping timer).
        Returns:
        Number of milliseconds elapsed
      • reportNano

        public long reportNano()
        Return elapsed time (without stopping timer).
        Returns:
        Number of nanoseconds elapsed
      • report

        public long report​(String str,
                           PrintStream stream)
        Print elapsed time (without stopping timer).
        Parameters:
        str - Additional prefix string to be printed
        stream - PrintStream on which to write output
        Returns:
        Number of milliseconds elapsed
      • report

        public long report​(String str)
        Print elapsed time to System.err (without stopping timer).
        Parameters:
        str - Additional prefix string to be printed
        Returns:
        Number of milliseconds elapsed
      • report

        public long report​(Redwood.RedwoodChannels logger,
                           String str)
        Print elapsed time (without stopping timer).
        Parameters:
        logger - Logger to log a timed operation with
        str - Additional prefix string to be printed
        Returns:
        Number of milliseconds elapsed
      • report

        public long report​(String str,
                           PrintWriter writer)
        Print elapsed time (without stopping timer).
        Parameters:
        str - Additional prefix string to be printed
        writer - PrintWriter on which to write output
        Returns:
        Number of milliseconds elapsed
      • toSecondsString

        public String toSecondsString()
        Returns the number of seconds passed since the timer started in the form "d.d".
      • toSecondsString

        public static String toSecondsString​(long elapsed)
        Format with one decimal place elapsed milliseconds in seconds.
        Parameters:
        elapsed - Number of milliseconds elapsed
        Returns:
        Formatted String
      • toMilliSecondsString

        public static String toMilliSecondsString​(long elapsed)
        Format with one decimal place elapsed milliseconds.
        Parameters:
        elapsed - Number of milliseconds elapsed
        Returns:
        Formatted String
      • restart

        public long restart()
        Restart timer.
        Returns:
        Number of milliseconds elapsed
      • restart

        public long restart​(String str,
                            PrintStream stream)
        Print elapsed time and restart timer.
        Parameters:
        str - Additional prefix string to be printed
        stream - PrintStream on which to write output
        Returns:
        Number of milliseconds elapsed
      • restart

        public long restart​(String str)
        Print elapsed time to System.err and restart timer.
        Parameters:
        str - Additional prefix string to be printed
        Returns:
        Number of milliseconds elapsed
      • restart

        public long restart​(String str,
                            PrintWriter writer)
        Print elapsed time and restart timer.
        Parameters:
        str - Additional prefix string to be printed
        writer - PrintWriter on which to write output
        Returns:
        Number of milliseconds elapsed
      • end

        public void end​(String msg)
        Print the timing done message with elapsed time in x.y seconds. Restart the timer too.
      • stop

        public long stop()
        Stop timer.
        Returns:
        Number of milliseconds elapsed
      • stop

        public long stop​(String str,
                         PrintStream stream)
        Print elapsed time and stop timer.
        Parameters:
        str - Additional prefix string to be printed
        stream - PrintStream on which to write output
        Returns:
        Number of milliseconds elapsed
      • stop

        public long stop​(String str)
        Print elapsed time to System.err and stop timer.
        Parameters:
        str - Additional prefix string to be printed
        Returns:
        Number of milliseconds elapsed
      • stop

        public long stop​(String str,
                         PrintWriter writer)
        Print elapsed time and stop timer.
        Parameters:
        str - Additional prefix string to be printed
        writer - PrintWriter on which to write output
        Returns:
        Number of milliseconds elapsed
      • startTime

        public static void startTime()
        Start (static) timer.
      • endTime

        public static long endTime()
        Return elapsed time on (static) timer (without stopping timer).
        Returns:
        Number of milliseconds elapsed
      • endTime

        public static long endTime​(String str,
                                   PrintStream stream)
        Print elapsed time on (static) timer (without stopping timer).
        Parameters:
        str - Additional prefix string to be printed
        stream - PrintStream on which to write output
        Returns:
        Number of milliseconds elapsed
      • endTime

        public static long endTime​(String str)
        Print elapsed time on (static) timer to System.err (without stopping timer).
        Parameters:
        str - Additional prefix string to be printed
        Returns:
        Number of milliseconds elapsed
      • doing

        public void doing​(String str)
        Print the start of timing message to stderr and start the timer.
      • done

        public void done()
        Finish the line from doing() with the end of the timing done message and elapsed time in x.y seconds.
      • done

        public void done​(String msg)
        Give a line saying that something is " done".
      • done

        public void done​(Redwood.RedwoodChannels logger,
                         StringBuilder msg)
        This method allows you to show the results of timing according to another class' logger. E.g., timing.done(logger, "Loading lexicon").
        Parameters:
        logger - Logger to log a timed operation with
        msg - Message to report.
      • startDoing

        public static void startDoing​(String str)
        Print the start of timing message to stderr and start the timer.
      • endDoing

        public static void endDoing()
        Finish the line from startDoing with the end of the timing done message and elapsed time in x.y seconds.
      • endDoing

        public static void endDoing​(String msg)
        Finish the line from startDoing with the end of the timing done message and elapsed time in x.y seconds.
      • tick

        public static long tick()
        Restart (static) timer.
        Returns:
        Number of milliseconds elapsed
      • tick

        public static long tick​(String str,
                                PrintStream stream)
        Print elapsed time and restart (static) timer.
        Parameters:
        str - Additional prefix string to be printed
        stream - PrintStream on which to write output
        Returns:
        Number of milliseconds elapsed
      • tick

        public static long tick​(String str)
        Print elapsed time to System.err and restart (static) timer.
        Parameters:
        str - Additional prefix string to be printed
        Returns:
        Number of milliseconds elapsed