Class StopWatch


  • public class StopWatch
    extends java.lang.Object
    Utility class to measure time differences in nano-seconds, based on System.nanoTime(). It compensates glitches in System.nanoTime(), such that the stop time can never be earlier than the start time. (For example, see stackoverflow)
    • Field Detail

      • time

        private long time
      • total

        private long total
      • started

        private long started
      • running

        private boolean running
      • format

        private static java.text.DecimalFormat format
    • Constructor Detail

      • StopWatch

        private StopWatch()
        Use createStopped() or createAndStart() to create a StopWatch. This constructor will also create a StopWatch, just like createStopped(), but the more expressively named factory methods are preferred.
    • Method Detail

      • createStopped

        public static StopWatch createStopped()
        Construct new StopWatch. It is not running initially. Call start() to start timing.
      • createAndStart

        public static StopWatch createAndStart()
        Construct and start a new StopWatch.
      • safeNanos

        private long safeNanos()
      • start

        public void start()
        Start the clock.
      • stop

        public void stop()
        Stop the clock.
      • nanoTime

        public long nanoTime()
        Get the total time the clock was running, in nano-seconds. Note that the clock can be started and stopped multiple times, accumulating the time intervals it was running in between.
        Returns:
        the total time the clock was running, in nano-seconds.
      • seconds

        public double seconds()
        Get the total time the clock was running in seconds. Note that the clock can be started and stopped multiple times, accumulating the time intervals it was running in between.
      • toString

        public java.lang.String toString()
        Get the total time the clock was running as string. Note that the clock can be started and stopped multiple times, accumulating the time intervals it was running in between.
        Overrides:
        toString in class java.lang.Object
      • secondsToString

        public static java.lang.String secondsToString​(double seconds)