KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > ext > locationserver > util > MicroTimer


1 package org.objectweb.proactive.ext.locationserver.util;
2
3 public class MicroTimer {
4
5   static {
6     System.loadLibrary("Util_timer_MicroTimer");
7   }
8
9
10   private long[] cumulatedTime;
11   private long[] startTime;
12   private long[] endTime;
13
14
15   public native long[] currentTime();
16
17
18   public MicroTimer() {
19
20   }
21
22
23   public void start() {
24     this.startTime = currentTime();
25     this.endTime = currentTime();
26     this.cumulatedTime = new long[2];
27   }
28
29
30   /**
31    * Stop the timer and
32    * returns the cumulated time
33    */

34
35   public void stop() {
36     this.endTime = currentTime();
37     //this.cumulatedTime = this.updateCumulatedTime(startTime, endTime);
38
//return this.cumulatedTime;
39

40   }
41
42
43  // /**
44
// * Resume the Microtimer
45
// * Doesn't reset the cumulatedTime value
46
// */
47
// public void resume() {
48
// this.startTime = currentTime();
49
// }
50

51
52   /**
53    * Return the newly computed cumulated time
54    * as measured by this MicroTimer
55    * in microseconds
56    */

57   public long getCumulatedTime() {
58       long[] tmp = this.updateCumulatedTime(startTime, endTime); //this.stop();
59

60     // this.resume();
61
return tmp[0] * 1000000 + tmp[1];
62   }
63
64
65   protected long[] updateCumulatedTime(long[] t1, long[] t2) {
66       long[] tmp = new long[2]; // this.cumulatedTime;
67
tmp[0] = t2[0] - t1[0];
68     // System.out.println(" updateCumulatedTime: " + t1[0] + " " + t1[1]);
69
// System.out.println(" with updateCumulatedTime: " + t2[0] + " " + t2[1]);
70
if (t2[1] - t1[1] < 0) {
71       //one second has gone
72
tmp[0]--;
73       tmp[1] = t2[1] + 1000000 - t1[1];
74         
75       //tmp[1]+=t1[1]-t2[1];
76
//tmp[0]++;
77
} else {
78       tmp[1] += t2[1] - t1[1];
79     }
80     return tmp;
81   }
82
83
84   public static void main(String JavaDoc[] args) {
85     MicroTimer timer = new MicroTimer();
86     long[] startTime;
87     long[] endTime;
88     int i = 0;
89     timer.start();
90     while (i < 10) {
91       // try {
92
// Thread.sleep(500);
93
// } catch (Exception e) {e.printStackTrace();}
94
System.out.println("Time is " + timer.getCumulatedTime());
95       i++;
96     }
97     timer.stop();
98     System.out.println("Sleeping 1s");
99     try {
100       Thread.sleep(1000);
101     } catch (Exception JavaDoc e) {
102       e.printStackTrace();
103     }
104     i = 0;
105     // timer.resume();
106
while (i < 10) {
107     long startTime2=System.currentTimeMillis();
108     timer.start();
109     try {
110         Thread.sleep(500);
111     } catch (Exception JavaDoc e) {e.printStackTrace();}
112     timer.stop();
113     long endTimeMicro = timer.getCumulatedTime();
114     long endTime2=System.currentTimeMillis();
115       System.out.println("Time is " + endTimeMicro);
116       System.out.println("Time in ms is " + (endTime2-startTime2));
117       i++;
118     }
119   }
120 }
121
Popular Tags