/Users/lyon/j4p/src/utils/Timer.java

1    package utils; 
2     
3    import java.io.PrintStream; 
4     
5    public class Timer { 
6        private long baseTime; 
7        private long elapsedTime; 
8     
9        private static double UNIT = 1000; 
10    
11       public Timer() { 
12           clear(); 
13       } 
14       /** 
15        * record the current time in milliseconds 
16        * at the start point. 
17        */ 
18       public void start() { 
19           baseTime = System.currentTimeMillis(); 
20       } 
21       /** 
22        * Zero the elapsedTime 
23        */ 
24       public void clear() { 
25           elapsedTime = 0; 
26       } 
27       /** 
28        * Compute the difference between the mark'd time 
29        * and the current time, in milliseconds. 
30        * The record method computes the elapsed_time. 
31        * 
32        */ 
33       public void stop() { 
34           elapsedTime = (System.currentTimeMillis() - baseTime); 
35       } 
36       /** 
37        * This is the time between a mark and a record. 
38        * @return the time in seconds as a float 
39        */ 
40       public double getElapsedTime() { 
41           return ((double) elapsedTime) / UNIT; 
42       } 
43       /** 
44        * Run the runnable <code>r</code> <code>n</code> 
45        * times and return the average time in ms. 
46        * @param r 
47        * @param n 
48        * @return 
49        */ 
50       public static long benchMark(Runnable r, int n){ 
51           Timer t = new Timer(); 
52           t.start(); 
53           for (int i=0; i < n; i++) { 
54               r.run(); 
55           } 
56           t.stop(); 
57           return t.elapsedTime /n; 
58    
59       } 
60    
61       public void report(PrintStream ps) { 
62           double elapsed_seconds = getElapsedTime(); 
63           ps.println("Time " + elapsed_seconds + " sec"); 
64       } 
65    
66       public static void main(String[] args) { 
67           Timer t = new Timer(); 
68           t.start(); 
69           double j = Math.PI; 
70           int n = 10000000; 
71           for (int i=0; i < n; i++){ 
72               j /= Math.E; 
73           } 
74           t.stop(); 
75    
76           double f = t.getElapsedTime(); 
77           double flops = n / f; 
78           System.out.println("you machine is running at "+flops/1000000 +" mflops"); 
79       } 
80       /** 
81        * Report the elapsed time between the mark and the record 
82        * invocations to the <code>System.out</code>. 
83        */ 
84       public void report() { 
85           report(System.out); 
86       } 
87    
88       public double getTime() { 
89           return getElapsedTime(); 
90       } 
91    
92       public void print(double N, String message) { 
93           elapsedTime = (System.currentTimeMillis() - baseTime); 
94           double s = elapsedTime; 
95           System.out.println( 
96                   message 
97                   + " " 
98                   + s + " ms " 
99                   + N / s + "  ops per ms"); 
100          baseTime = System.currentTimeMillis(); 
101      } 
102   
103      public void print(String message) { 
104   
105          System.out.println( 
106                  message 
107                  + " " 
108                  + getTime() + " seconds " 
109          ); 
110          start(); 
111      } 
112   
113  } 
114   
115