Package com.fizzed.crux.util
Class StopWatch
- java.lang.Object
-
- com.fizzed.crux.util.StopWatch
-
public class StopWatch extends java.lang.ObjectLightweight 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
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description doubleelapsed()Returns the elapsed time based on the time unit of this stopwatch.doubleelapsed(java.util.concurrent.TimeUnit tu)Returns the elapsed time for the specified time unit.doubleelapsedDays()Returns the total time elapsed in days.doubleelapsedHours()Returns the total time elapsed in hours.doubleelapsedMicros()Returns the total time elapsed in microseconds.doubleelapsedMillis()Returns the total time elapsed in milliseconds.doubleelapsedMinutes()Returns the total time elapsed in minutes.longelapsedNanos()doubleelapsedSeconds()Returns the total time elapsed in seconds.booleaneq(double duration)booleangt(double duration)booleangte(double duration)booleanisRunning()If still running (not stopped yet)booleanisStopped()booleanlt(double duration)booleanlte(double duration)StopWatchstop()static StopWatchtime(java.util.concurrent.TimeUnit tu)static StopWatchtime(java.util.concurrent.TimeUnit tu, java.lang.Runnable runnable)static StopWatchtimeMillis()static StopWatchtimeMillis(java.lang.Runnable runnable)static StopWatchtimeNanos()static StopWatchtimeNanos(java.lang.Runnable runnable)static StopWatchtimeSeconds()static StopWatchtimeSeconds(java.lang.Runnable runnable)java.lang.StringtoString()Returns the current elapsed time in a format determined by the default time unit of this instance.java.lang.StringtoString(java.util.concurrent.TimeUnit tu)Returns the number of seconds this Stopwatch has elapsed
-
-
-
Method Detail
-
isRunning
public boolean isRunning()
If still running (not stopped yet)- Returns:
- True if running or false if stopped
-
isStopped
public boolean isStopped()
-
stop
public StopWatch stop()
-
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:
toStringin classjava.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
-
-