Class Stopwatch
Starting timers
You can have as many timers running simultaneously as you wish, memory permitting. Each time you invoke Stopwatch.start() with a unique timer string id, a new timer is added. If you invoke Stopwatch.start() with the id of a timer that is already in progress, that timer is restarted and its previous value is lost.
Monitoring timers in progress
You can report on the status of a running timer without stopping it by invoking the report() or reportFormatted() method. You can check to see if a timer is currently running using the isRunning() method, and you can see how many timers are currently running via the getTimerCount() method.
Stopping a timer
Calling Stopwatch.stop() will stop a timer and mark how much time it counted while it was running. At any point after stopping a timer, you can invoke report() or reportFormatted() to get this information. A stopped timer can be restarted by passing its id to the start() method.
Formatting timing information
The raw timer results can be retrieved by report(), which simply returns the number of milliseconds for which the timer in question ran. If you want a more human-readable version of this information, you can invoke reportFormatted() and get a friendlier string version, along the lines of "1h24m32s". Note that fractional second values are ignored by reportFormatted(). If higher precision is required, use the report() method instead.
- Since:
- 2023-03-17
- Author:
- scorbo2
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic StringformatTimeValue(long timeValue) Takes a count of milliseconds and returns it in a human-readable format along the lines of "1h24m32s".static intReturns a count of how many timers are currently running.static booleanIndicates whether a timer with the given id is currently running.static longReports on the timer with the given identifier.static StringReports on the timer with the given identifier.static voidStarts a timer with the given identifier.static longStops the timer with the given identifier, and notes the count of milliseconds for which that timer was running.static intstopAll()Stops all timers, and returns a count of how many timers were affected.
-
Constructor Details
-
Stopwatch
public Stopwatch()
-
-
Method Details
-
start
Starts a timer with the given identifier. If a timer with that identifier is already running, it is restarted.- Parameters:
id- Any String which uniquely identifies this timer. Null values are ignored.
-
stop
Stops the timer with the given identifier, and notes the count of milliseconds for which that timer was running. The elapsed time in milliseconds is returned from this method, but can also be queried via the report methods. If the given id does not reference a currently running timer, this method does nothing and will return 0.- Parameters:
id- The unique String identifier of the timer in question.- Returns:
- The number of milliseconds for which the timer in question was running.
-
stopAll
public static int stopAll()Stops all timers, and returns a count of how many timers were affected.- Returns:
- How many timers were stopped by this call.
-
getTimerCount
public static int getTimerCount()Returns a count of how many timers are currently running.- Returns:
- How many timers are currently running.
-
isRunning
Indicates whether a timer with the given id is currently running.- Parameters:
id- The identifier of the timer in question.- Returns:
- true if the named timer exists and is running.
-
report
Reports on the timer with the given identifier. If the identifier refers to a timer that is currently running, then the return value is the number of milliseconds for which the timer in question has been running. The timer will not be stopped as a result of this call. If the identifier refers to a timer that has been stopped, then the result is the number of milliseconds for which that timer ran before it was stopped. If the given identifier does not match any known timer, then this method does nothing and will return 0.- Parameters:
id- The identifier of the timer in question.- Returns:
- A count of milliseconds for the given timer, as described above.
-
reportFormatted
Reports on the timer with the given identifier. This is similar to the report method except that the return value here is a formatted, human-readable string describing the elapsed time. For example: "1m24s" instead of 84000.- Parameters:
id- The identifier of the timer in question.- Returns:
- A human-readable string representing the elapsed time.
-
formatTimeValue
Takes a count of milliseconds and returns it in a human-readable format along the lines of "1h24m32s". Extremely short values (less than one second) are returned in the format "44ms".- Parameters:
timeValue- An arbitrary millisecond count.- Returns:
- A formatted String that describes how long the given timeValue is.
-