1 19 20 25 26 27 package soot; 28 import soot.options.*; 29 30 import soot.util.*; 31 import java.util.*; 32 33 35 public class Timer 36 { 37 private long duration; 38 private long startTime; 39 private boolean hasStarted; 40 41 private String name; 42 43 44 45 public Timer(String name) 46 { 47 this.name = name; 48 duration = 0; 49 } 50 51 52 public Timer() 53 { 54 this("unnamed"); 55 } 56 57 58 public void start() 59 { 60 if(!G.v().Timer_isGarbageCollecting && Options.v() != null && Options.v().subtract_gc() && ((G.v().Timer_count++ % 4) == 0)) 62 { 63 65 G.v().Timer_isGarbageCollecting = true; 66 67 G.v().Timer_forcedGarbageCollectionTimer.start(); 68 69 { 71 Iterator timerIt = G.v().Timer_outstandingTimers.iterator(); 72 73 while(timerIt.hasNext()) 74 { 75 Timer t = (Timer) timerIt.next(); 76 77 t.end(); 78 } 79 } 80 81 System.gc(); 82 83 { 85 Iterator timerIt = G.v().Timer_outstandingTimers.iterator(); 86 87 while(timerIt.hasNext()) 88 { 89 Timer t = (Timer) timerIt.next(); 90 91 t.start(); 92 } 93 } 94 95 G.v().Timer_forcedGarbageCollectionTimer.end(); 96 97 G.v().Timer_isGarbageCollecting = false; 98 } 99 100 101 startTime = System.currentTimeMillis(); 102 103 if(hasStarted) 104 throw new RuntimeException ("timer " + name + " has already been started!"); 105 else 106 hasStarted = true; 107 108 109 if(!G.v().Timer_isGarbageCollecting) 110 { 111 G.v().Timer_outstandingTimers.add(this); 112 } 113 114 } 115 116 117 public String toString() 118 { 119 return name; 120 } 121 122 123 public void end() 124 { 125 if(!hasStarted) 126 throw new RuntimeException ("timer " + name + " has not been started!"); 127 else 128 hasStarted = false; 129 130 duration += System.currentTimeMillis() - startTime; 131 132 133 if(!G.v().Timer_isGarbageCollecting) 134 { 135 G.v().Timer_outstandingTimers.remove(this); 136 } 137 } 138 139 140 public long getTime() 141 { 142 return duration; 143 } 144 } 145 146 147 148 149 | Popular Tags |