TizzyT

StopWatch -TizzyT

Sep 20th, 2017
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.40 KB | None | 0 0
  1. // Stopwatch which utilizes high-resolution performance counters for more precision
  2. public class StopWatch {
  3.     private long start, stop;
  4.     private boolean running;
  5.  
  6.     // The duration in days the stopwatch has elapsed
  7.     public double elapsedDays() {
  8.         if (running) return (System.nanoTime() - start) / 86400000000000.0;
  9.         return (stop - start) / 86400000000000.0;
  10.     }
  11.  
  12.     // The duration in hours the stopwatch has elapsed
  13.     public double elapsedHours() {
  14.         if (running) return (System.nanoTime() - start) / 3600000000000.0;
  15.         return (stop - start) / 3600000000000.0;
  16.     }
  17.  
  18.     // The duration in minutes the stopwatch has elapsed
  19.     public double elapsedMinutes() {
  20.         if (running) return (System.nanoTime() - start) / 60000000000.0;
  21.         return (stop - start) / 60000000000.0;
  22.     }
  23.  
  24.     // The duration in seconds the stopwatch has elapsed
  25.     public double elapsedSeconds() {
  26.         if (running) return (System.nanoTime() - start) / 1000000000.0;
  27.         return (stop - start) / 1000000000.0;
  28.     }
  29.  
  30.     // The duration in milliseconds the stopwatch has elapsed
  31.     public double elapsedMilliseconds() {
  32.         if (running) return (System.nanoTime() - start) / 1000000.0;
  33.         return (stop - start) / 1000000.0;
  34.     }
  35.  
  36.     // The duration in microseconds the stopwatch has elapsed
  37.     public double elapsedMicroseconds() {
  38.         if (running) return (System.nanoTime() - start) / 1000.0;
  39.         return (stop - start) / 1000.0;
  40.     }
  41.  
  42.     // The duration in nanoseconds the stopwatch has elapsed
  43.     public double elapsedNanoseconds() {
  44.         if (running) return (System.nanoTime() - start);
  45.         return stop - start;
  46.     }
  47.  
  48.     // Starts the StopWatch and keeps time
  49.     public void Start() {
  50.         if (!running) {
  51.             start = System.nanoTime() - (stop - start);
  52.             running = true;
  53.         }
  54.     }
  55.  
  56.     // Stops the StopWatch from keeping time
  57.     public void Stop() {
  58.         if (running) {
  59.             stop = System.nanoTime();
  60.             running = false;
  61.         }
  62.     }
  63.  
  64.     // Resets the time at which the StopWatch started keeping time
  65.     public void Reset() {
  66.         start = System.nanoTime();
  67.     }
  68.  
  69.     // Resets the time at which the StopWatch started keeping time and also starts keeping time
  70.     public void Restart() {
  71.         start = System.nanoTime();
  72.         running = true;
  73.     }
  74. }
Add Comment
Please, Sign In to add comment