brilliant_moves

TestNanoTimer2.java

Oct 2nd, 2015
329
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 1.27 KB | None | 0 0
  1. public class TestNanoTimer2 {
  2.  
  3.   /**
  4.     *   Program:    TestNanoTimer2.java
  5.     *   Purpose:    Time running of a for loop
  6.     *   Creator:    Chris Clarke
  7.     *   Created:    02.10.2015
  8.     */
  9.  
  10.     private static final int MAX = 10000000; // ten million
  11.  
  12.     public static void main(String[] args) {
  13.  
  14.         NanoTimer2 t = new NanoTimer2();
  15.         t.setStartTime();
  16.  
  17.         // start of test loop
  18.  
  19.         for (int i=0; i<MAX; i++) {
  20.             double j = i * 2.12345;
  21.         } // end for
  22.  
  23.         // end of test loop
  24.  
  25.         t.setRunningTime();
  26.         System.out.println(t.toString());
  27.  
  28.     } // end main()
  29.  
  30. } // end class testNanoTimer2
  31.  
  32.  
  33. class NanoTimer2 {
  34.  
  35.    /*
  36.     *   Class:      NanoTimer2.java
  37.     *   Purpose:    Get program running times
  38.     *   Creator:    Chris Clarke
  39.     *   Created:    03.04.2014 and 02.10.2015
  40.     */
  41.  
  42.     private long startTime = 0, runningTime = 0;
  43.  
  44.     public void setStartTime() {
  45.         startTime = System.nanoTime();
  46.     }
  47.  
  48.     public void setRunningTime() {
  49.         runningTime = System.nanoTime() - startTime;
  50.     }
  51.  
  52.     public long getRunningTime() {
  53.         return runningTime;
  54.     }
  55.  
  56.     public String toString() {
  57.         return "Running time: " + getRunningTime() + " ns";
  58.     }
  59.  
  60. } // end class NanoTimer2
Advertisement
Add Comment
Please, Sign In to add comment