Advertisement
brilliant_moves

AlgorithmRunningTime.java

Oct 2nd, 2015
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 1.04 KB | None | 0 0
  1. public class AlgorithmRunningTime {
  2.  
  3.     public static void main(String[] args) {
  4.  
  5.         final int N = 2000000; // test data
  6.  
  7.         NanoTimer t = new NanoTimer();
  8.         long startTime, runningTime;
  9.         int sum = 0;
  10.  
  11.         startTime = t.getStartTime();
  12.         for (int i = 1; i < N; i*=2){
  13.             for (int j = 0; j < i; j++){
  14.                 sum++;
  15.             } // for j
  16.         } // for i
  17.         runningTime = t.getRunningTime();
  18.         System.out.println(t.toString());
  19.     } // main()
  20.  
  21. } // class AlgorithmRunningTime
  22.  
  23.  
  24. class NanoTimer {
  25.  
  26.    /*
  27.     *   Class:      NanoTimer.java
  28.     *   Purpose:    Get program running times
  29.     *   Creator:    Chris Clarke
  30.     *   Created:    03.04.2014
  31.     */
  32.  
  33.     public long startTime = 0, runningTime = 0;
  34.  
  35.     public long getStartTime() {
  36.         startTime = System.nanoTime();
  37.         return startTime;
  38.     }
  39.  
  40.     public long getRunningTime() {
  41.         runningTime = System.nanoTime() - startTime;
  42.         return runningTime;
  43.     }
  44.  
  45.     public String toString() {
  46.         return "Running time: " + runningTime + " ns";
  47.     }
  48.  
  49. } // end class NanoTimer
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement