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 34 35 public void stop() { 36 this.endTime = currentTime(); 37 40 } 41 42 43 51 52 57 public long getCumulatedTime() { 58 long[] tmp = this.updateCumulatedTime(startTime, endTime); 60 return tmp[0] * 1000000 + tmp[1]; 62 } 63 64 65 protected long[] updateCumulatedTime(long[] t1, long[] t2) { 66 long[] tmp = new long[2]; tmp[0] = t2[0] - t1[0]; 68 if (t2[1] - t1[1] < 0) { 71 tmp[0]--; 73 tmp[1] = t2[1] + 1000000 - t1[1]; 74 75 } else { 78 tmp[1] += t2[1] - t1[1]; 79 } 80 return tmp; 81 } 82 83 84 public static void main(String [] args) { 85 MicroTimer timer = new MicroTimer(); 86 long[] startTime; 87 long[] endTime; 88 int i = 0; 89 timer.start(); 90 while (i < 10) { 91 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 e) { 102 e.printStackTrace(); 103 } 104 i = 0; 105 while (i < 10) { 107 long startTime2=System.currentTimeMillis(); 108 timer.start(); 109 try { 110 Thread.sleep(500); 111 } catch (Exception 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 |