Class StopWatch


  • public class StopWatch
    extends java.lang.Object
    A utility class for managing a "wall clock" timer.

    The timer starts when you create the object. Subsequent calls will return information based on the elapsed wall-clock time since the object was created. If stop is called, it will fix the end time.

    • Constructor Summary

      Constructors 
      Constructor Description
      StopWatch()
      Create a stopwatch.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      long duration()
      The number of milliseconds that the stopwatch has been running.
      java.lang.String elapsed()
      The elapsed time in a human-friendly format.
      java.lang.String elapsed​(long duration)
      The elapsed time in a human-friendly format.
      java.lang.String perSecond​(long events)
      Compute events per second.
      void stop()
      Stop the watch.
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • StopWatch

        public StopWatch()
        Create a stopwatch.

        Time begins counting immediately.

    • Method Detail

      • stop

        public void stop()
        Stop the watch.

        After calling this method, the time will remain fixed.

      • duration

        public long duration()
        The number of milliseconds that the stopwatch has been running.

        Or, if stop has been called, how long it ran.

        Returns:
        the number of milliseconds
      • perSecond

        public java.lang.String perSecond​(long events)
        Compute events per second.

        Computes events per second for the given number of events. It returns a string of the form %3.1f.

        Parameters:
        events - The number of events
        Returns:
        A formatted string
      • elapsed

        public java.lang.String elapsed()
        The elapsed time in a human-friendly format.

        This method uses the timer's elapsed time.

        Returns:
        A formatted string
      • elapsed

        public java.lang.String elapsed​(long duration)
        The elapsed time in a human-friendly format.

        Returns the number of days, minutes, hours, and seconds represented by the duration, a number of milliseconds. For example, 103,010,000ms is "1d4h36m50s".

        Parameters:
        duration - the duration in milliseconds
        Returns:
        A formatted string