Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Stopwatch which utilizes high-resolution performance counters for more precision
- public class StopWatch {
- private long start, stop;
- private boolean running;
- // The duration in days the stopwatch has elapsed
- public double elapsedDays() {
- if (running) return (System.nanoTime() - start) / 86400000000000.0;
- return (stop - start) / 86400000000000.0;
- }
- // The duration in hours the stopwatch has elapsed
- public double elapsedHours() {
- if (running) return (System.nanoTime() - start) / 3600000000000.0;
- return (stop - start) / 3600000000000.0;
- }
- // The duration in minutes the stopwatch has elapsed
- public double elapsedMinutes() {
- if (running) return (System.nanoTime() - start) / 60000000000.0;
- return (stop - start) / 60000000000.0;
- }
- // The duration in seconds the stopwatch has elapsed
- public double elapsedSeconds() {
- if (running) return (System.nanoTime() - start) / 1000000000.0;
- return (stop - start) / 1000000000.0;
- }
- // The duration in milliseconds the stopwatch has elapsed
- public double elapsedMilliseconds() {
- if (running) return (System.nanoTime() - start) / 1000000.0;
- return (stop - start) / 1000000.0;
- }
- // The duration in microseconds the stopwatch has elapsed
- public double elapsedMicroseconds() {
- if (running) return (System.nanoTime() - start) / 1000.0;
- return (stop - start) / 1000.0;
- }
- // The duration in nanoseconds the stopwatch has elapsed
- public double elapsedNanoseconds() {
- if (running) return (System.nanoTime() - start);
- return stop - start;
- }
- // Starts the StopWatch and keeps time
- public void Start() {
- if (!running) {
- start = System.nanoTime() - (stop - start);
- running = true;
- }
- }
- // Stops the StopWatch from keeping time
- public void Stop() {
- if (running) {
- stop = System.nanoTime();
- running = false;
- }
- }
- // Resets the time at which the StopWatch started keeping time
- public void Reset() {
- start = System.nanoTime();
- }
- // Resets the time at which the StopWatch started keeping time and also starts keeping time
- public void Restart() {
- start = System.nanoTime();
- running = true;
- }
- }
Add Comment
Please, Sign In to add comment