Class StopWatch


  • public class StopWatch
    extends java.lang.Object
    Lightweight timing for a little bit of anything. Uses System.nanoTime() under-the-hood for highly precise measurements. NOTE: nothing is synchronized and should ideally only be used within a single thread.
    Author:
    joelauer
    • Constructor Summary

      Constructors 
      Constructor Description
      StopWatch()
      Creates a stopwatch with a default time unit of milliseconds.
      StopWatch​(java.util.concurrent.TimeUnit tu)
      Creates a stopwatch with the specified time unit.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      double elapsed()
      Returns the elapsed time based on the time unit of this stopwatch.
      double elapsed​(java.util.concurrent.TimeUnit tu)
      Returns the elapsed time for the specified time unit.
      double elapsedDays()
      Returns the total time elapsed in days.
      double elapsedHours()
      Returns the total time elapsed in hours.
      double elapsedMicros()
      Returns the total time elapsed in microseconds.
      double elapsedMillis()
      Returns the total time elapsed in milliseconds.
      double elapsedMinutes()
      Returns the total time elapsed in minutes.
      long elapsedNanos()  
      double elapsedSeconds()
      Returns the total time elapsed in seconds.
      boolean eq​(double duration)  
      boolean gt​(double duration)  
      boolean gte​(double duration)  
      boolean isRunning()
      If still running (not stopped yet)
      boolean isStopped()  
      boolean lt​(double duration)  
      boolean lte​(double duration)  
      StopWatch stop()  
      static StopWatch time​(java.util.concurrent.TimeUnit tu)  
      static StopWatch time​(java.util.concurrent.TimeUnit tu, java.lang.Runnable runnable)  
      static StopWatch timeMillis()  
      static StopWatch timeMillis​(java.lang.Runnable runnable)  
      static StopWatch timeNanos()  
      static StopWatch timeNanos​(java.lang.Runnable runnable)  
      static StopWatch timeSeconds()  
      static StopWatch timeSeconds​(java.lang.Runnable runnable)  
      java.lang.String toString()
      Returns the current elapsed time in a format determined by the default time unit of this instance.
      java.lang.String toString​(java.util.concurrent.TimeUnit tu)
      Returns the number of seconds this Stopwatch has elapsed
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • StopWatch

        public StopWatch()
        Creates a stopwatch with a default time unit of milliseconds.
      • StopWatch

        public StopWatch​(java.util.concurrent.TimeUnit tu)
        Creates a stopwatch with the specified time unit.
        Parameters:
        tu - The time unit for formatting and comparison
    • Method Detail

      • isRunning

        public boolean isRunning()
        If still running (not stopped yet)
        Returns:
        True if running or false if stopped
      • isStopped

        public boolean isStopped()
      • timeMillis

        public static StopWatch timeMillis()
      • timeMillis

        public static StopWatch timeMillis​(java.lang.Runnable runnable)
      • timeNanos

        public static StopWatch timeNanos()
      • timeNanos

        public static StopWatch timeNanos​(java.lang.Runnable runnable)
      • timeSeconds

        public static StopWatch timeSeconds()
      • timeSeconds

        public static StopWatch timeSeconds​(java.lang.Runnable runnable)
      • time

        public static StopWatch time​(java.util.concurrent.TimeUnit tu)
      • time

        public static StopWatch time​(java.util.concurrent.TimeUnit tu,
                                     java.lang.Runnable runnable)
      • elapsed

        public double elapsed()
        Returns the elapsed time based on the time unit of this stopwatch.
        Returns:
        The elapsed time
      • elapsed

        public double elapsed​(java.util.concurrent.TimeUnit tu)
        Returns the elapsed time for the specified time unit.
        Parameters:
        tu - The time unit
        Returns:
        The elapsed time
      • eq

        public boolean eq​(double duration)
      • gt

        public boolean gt​(double duration)
      • gte

        public boolean gte​(double duration)
      • lt

        public boolean lt​(double duration)
      • lte

        public boolean lte​(double duration)
      • elapsedNanos

        public long elapsedNanos()
      • elapsedMicros

        public double elapsedMicros()
        Returns the total time elapsed in microseconds. If the timer is still running then this is based on current time.
        Returns:
        The total elapsed time in microseconds (mu)
      • elapsedMillis

        public double elapsedMillis()
        Returns the total time elapsed in milliseconds. If the timer is still running then this is based on current time.
        Returns:
        The total elapsed time in milliseconds (ms)
      • elapsedSeconds

        public double elapsedSeconds()
        Returns the total time elapsed in seconds. If the timer is still running then this is based on current time.
        Returns:
        The total elapsed time in seconds (s)
      • elapsedMinutes

        public double elapsedMinutes()
        Returns the total time elapsed in minutes. If the timer is still running then this is based on current time.
        Returns:
        The total elapsed time in minutes (m)
      • elapsedHours

        public double elapsedHours()
        Returns the total time elapsed in hours. If the timer is still running then this is based on current time.
        Returns:
        The total elapsed time in hours (h)
      • elapsedDays

        public double elapsedDays()
        Returns the total time elapsed in days. If the timer is still running then this is based on current time.
        Returns:
        The total elapsed time in days (d)
      • toString

        public java.lang.String toString()
        Returns the current elapsed time in a format determined by the default time unit of this instance.
        Overrides:
        toString in class java.lang.Object
        Returns:
        A string of the current elapsed time such as "10.1 ms" or "0.12 s"
      • toString

        public java.lang.String toString​(java.util.concurrent.TimeUnit tu)
        Returns the number of seconds this Stopwatch has elapsed
        Returns:
        The String of the number of seconds