Timer - Sam36502/SimpleTUI GitHub Wiki
What The Timer Object Does
This object was intended to simplify benchmarking. It's great to quickly test how efficient something is. Other than that, it can be used for general timing purposes, such as telling the user how long they were using the software for, or how long a download took.
Timing
To begin the timer, simply create a new timer, and start it with
start()
:
Timer t = new Timer();
t.start();
The timer will immediately start counting. To stop it again
use t.stop();
. If you want to resume timing, while keeping
the old end-time, simply t.start();
it again. If you want
to use the same timer but reset it to 0, you can use t.reset();
;
Getting Measurements
To find out how much time has elapsed, you can either get
the number of milliseconds directly with t.getMillis()
or you can get a neatly formatted timestamp with t.getTimestamp();
.
This will be in the format "HH:mm:ss"
Example:
Timer t = new Timer();
t.start();
// some code that takes 4 seconds to run
t.stop();
t.getMillis(); // Returns 4000
t.start();
// some code that takes 5 minutes to run
t.stop();
t.getTimestamp(); // Returns "00:05:04"
t.reset();
t.getTimestamp(); // Returns "00:00:00"