Class NestedTimer


  • public final class NestedTimer
    extends Object
    This class represents a timer similar to Timer, however it nests two timers.

    If you have a method A that calls a method B, this class can be used to measure (1) the time spent by A and B together, (2) the time spent by B, and (3) the time spent only in A (without the call to B). The advantage over using two separate timers is that this allows to compute average and maximum of measurements for (3). A simple use of two timers would allow to compute these values only for (1) and (2); for (3) one could only compute the sum time.

    This class uses the name "total" to refer to measurement (1), the name "inner" to refer to measurement (2), and the name "outer" to refer to measurement (3). So in general "total" represents the sum of "inner" and "outer".

    The "inner" timer can never be running as long as the "total" timer is not running. During a single "total" interval, the "inner" timer can be started and stopped several times. This will be counted only as one "inner" interval (e.g., when the average "inner" time is computed). Similarly, when the "inner" timer is started and stopped several times in a single "total" interval, this counts as only one "outer" interval. The number of all starts and stops of the "inner" timer summed over all "outer" intervals is never used by this class and is not available to the user.

    This class is not thread-safe and may be used only from within a single thread.

    • Constructor Detail

      • NestedTimer

        public NestedTimer()
    • Method Detail

      • startOuter

        public void startOuter()
        Start the outer timer. May be called only if the timer is currently not running.
      • startBoth

        public void startBoth()
        Start both timers. May be called only if the timer is currently not running. Guarantees that both timers are started in the exact same instant of time.
      • stopOuter

        public void stopOuter()
        Stop the outer timer. May be called only if the outer timer is currently running and the inner timer is stopped.
      • stopBoth

        public void stopBoth()
      • isRunning

        public boolean isRunning()
        Check if the timer is running. Contrary to the other methods of this class, this method is thread-safe. This means it can be safely run from another thread.
      • isOnlyOuterRunning

        public boolean isOnlyOuterRunning()
        Check if the outer timer is running, i.e., the timer is running but the inner timer is not running. Contrary to the other methods of this class, this method is thread-safe. This means it can be safely run from another thread.
      • getCurentInnerTimer

        public Timer getCurentInnerTimer()
        Get a reference to the current inner timer. This reference can be used to start and stop the inner interval (even multiple times). The returned reference becomes invalid after a call to stopOuter() or stopBoth() and may not be used anymore afterwards.
      • getOuterSumTime

        public TimeSpan getOuterSumTime()
        Return the sum of all outer intervals. If the outer timer is running, the current interval is also counted (up to the current time). If the timer was never started, this method returns 0.
      • getInnerSumTime

        public TimeSpan getInnerSumTime()
        Return the sum of all inner intervals. If the inner timer is running, the current interval is also counted (up to the current time). If the timer was never started, this method returns 0. To get only the sum time of the inner timer for the interval since the last time the outer timer was started, call getInnerTimer().getSumTime().
      • getTotalSumTime

        public TimeSpan getTotalSumTime()
        Return the sum of all total intervals. If the outer timer is running, the current interval is also counted (up to the current time). If the timer was never started, this method returns 0.
      • getOuterMaxTime

        public TimeSpan getOuterMaxTime()
        Return the maximal time of all outer intervals. If the outer timer is running, the current interval is also counted (up to the current time). If the timer was never started, this method returns 0.
      • getInnerMaxTime

        public TimeSpan getInnerMaxTime()
        Return the maximal time of all inner intervals. Note that this is not the same as the maximum of the intervals of all inner timers. If the inner timer is running, the current interval is also counted (up to the current time). If the timer was never started, this method returns 0.
      • getTotalMaxTime

        public TimeSpan getTotalMaxTime()
        Return the maximal time of all total intervals. If the outer timer is running, the current interval is also counted (up to the current time). If the timer was never started, this method returns 0.
      • getNumberOfIntervals

        public int getNumberOfIntervals()
        Return the number of total intervals. Note that this is the same as the number of outer intervals. If timer is running, the current interval is also counted. If the timer was never started, this method returns 0.
      • getLengthOfLastOuterInterval

        public TimeSpan getLengthOfLastOuterInterval()
        Return the length of the last measured outer interval. If the outer timer is running, this is the time from the start of the current interval up to now. If the timer was never started, this method returns 0.
      • getLengthOfLastTotalInterval

        public TimeSpan getLengthOfLastTotalInterval()
        Return the length of the last measured total interval. If the timer is running, this is the time from the start of the current interval up to now. If the timer was never started, this method returns 0.
      • getOuterAvgTime

        public TimeSpan getOuterAvgTime()
        Return the average of all outer intervals. If the outer timer is running, the current interval is also counted (up to the current time). If the timer was never started, this method returns 0.
      • getInnerAvgSumTime

        public TimeSpan getInnerAvgSumTime()
        Return the average of all inner times. Note that this is not the average of all intervals of the inner timers, but basically the same as getInnerSumTime() / getNumberOfIntervals() . If the inner timer is running, the current interval is also counted (up to the current time). If the timer was never started, this method returns 0.
      • getTotalAvgTime

        public TimeSpan getTotalAvgTime()
        Return the average of all total intervals. If the timer is running, the current interval is also counted (up to the current time). If the timer was never started, this method returns 0.
      • toString

        public String toString()
        Return a String with a default representation of the the sum of the times of total intervals. For printing other times, or with a specific unit, use the appropriate getter and call TimeSpan.formatAs(java.util.concurrent.TimeUnit). The format and the content of the String returned by this method is not guaranteed to be the same in future versions of this code.
        Overrides:
        toString in class Object