1 | package tuffy.util; |
2 | import java.text.DateFormat; |
3 | import java.text.SimpleDateFormat; |
4 | import java.util.ArrayList; |
5 | import java.util.Date; |
6 | import java.util.Hashtable; |
7 | |
8 | /** |
9 | * Container of time related utilities. |
10 | */ |
11 | public class Timer { |
12 | |
13 | private static long birth = now(); |
14 | private static Hashtable<String, Long> points = new Hashtable<String, Long>(); |
15 | |
16 | private static long secondInMillis = 1000; |
17 | private static long minuteInMillis = secondInMillis * 60; |
18 | |
19 | /** |
20 | * Resets the global starting point of the Timer. |
21 | */ |
22 | public static void resetClock() { |
23 | birth = now(); |
24 | } |
25 | |
26 | public static String getDateTime(){ |
27 | DateFormat df = new SimpleDateFormat("H:mm:ss M/d/yy"); |
28 | return df.format(new Date()); |
29 | } |
30 | |
31 | /** |
32 | * Kicks off a timer with the given name. |
33 | */ |
34 | public static void start(String name) { |
35 | points.put(name, now()); |
36 | } |
37 | |
38 | /** |
39 | * Returns the elapsed time of the timer with the given name. |
40 | * The format is [XX minutes, YYseconds]. |
41 | */ |
42 | public static String elapsed(String name) { |
43 | if(!points.containsKey(name)) { |
44 | return "[UNKNOWN TIMER]"; |
45 | }else { |
46 | long diff = now() - points.get(name); |
47 | return readTime(diff); |
48 | } |
49 | } |
50 | |
51 | /** |
52 | * Returns the number of elapsed seconds of a given timer; |
53 | * -1 if the timer is unknown. |
54 | * @param name the name of the timer |
55 | */ |
56 | public static double elapsedSeconds(String name) { |
57 | if(!points.containsKey(name)) { |
58 | return -1; |
59 | }else { |
60 | long diff = now() - points.get(name); |
61 | return (diff / 1000); |
62 | } |
63 | } |
64 | |
65 | public static double elapsedMilliSeconds(String name) { |
66 | if(!points.containsKey(name)) { |
67 | return -1; |
68 | }else { |
69 | long diff = now() - points.get(name); |
70 | return diff; |
71 | } |
72 | } |
73 | |
74 | /** |
75 | * Returns the number of elapsed seconds since last clock reset. |
76 | */ |
77 | public static double elapsedSeconds() { |
78 | long diff = now() - birth; |
79 | return (diff / 1000.0); |
80 | } |
81 | |
82 | /** |
83 | * Prints the elapsed time since last clock reset. |
84 | * The format is TIMER: [XX minutes, YYseconds]. |
85 | */ |
86 | public static void printElapsed() { |
87 | UIMan.println("TIMER: " + elapsed()); |
88 | } |
89 | |
90 | /** |
91 | * Prints the elapsed time of the timer with the given name. |
92 | * The format is TIMER name: [XX minutes, YYseconds]. |
93 | */ |
94 | public static void printElapsed(String name) { |
95 | UIMan.println("TIMER " + name + ": " + elapsed(name)); |
96 | } |
97 | |
98 | private static long now() { |
99 | return System.currentTimeMillis(); |
100 | } |
101 | |
102 | /** |
103 | * Returns a string of elapsed time. |
104 | */ |
105 | public static String elapsed() { |
106 | return readTime(now() - birth); |
107 | } |
108 | |
109 | private static String readTime(long timeIntervalInMS) { |
110 | long min = timeIntervalInMS / minuteInMillis; |
111 | long sec = timeIntervalInMS / secondInMillis % 60; |
112 | long mm = timeIntervalInMS % 1000; |
113 | String t = String.format("[%d min, %d.%03d sec]", min, sec, mm); |
114 | return t; |
115 | } |
116 | |
117 | public static RunStat runStat = new RunStat(); |
118 | |
119 | public static class RunStat{ |
120 | public double groundSec = 0; |
121 | public double inferSec = 0; |
122 | public double inferOps = 0; |
123 | public long effectiveSteps = 0; |
124 | |
125 | public ArrayList<Double> turns = new ArrayList<Double>(); |
126 | public ArrayList<Double> costs = new ArrayList<Double>(); |
127 | |
128 | public void addTurn(double cost){ |
129 | turns.add(Timer.elapsedSeconds()); |
130 | costs.add(cost); |
131 | } |
132 | |
133 | public double getGroundTime(){ |
134 | return groundSec; |
135 | } |
136 | |
137 | public double getFlipRate(){ |
138 | return inferOps / inferSec; |
139 | } |
140 | |
141 | public void markGroundingDone(){ |
142 | groundSec = Timer.elapsedSeconds(); |
143 | } |
144 | |
145 | public void markInferDone(){ |
146 | inferSec = Timer.elapsedSeconds() - groundSec; |
147 | } |
148 | |
149 | public void setInferOps(long ops){ |
150 | inferOps = ops; |
151 | } |
152 | |
153 | public void report(){ |
154 | System.out.println("============================"); |
155 | boolean report_trace = false; |
156 | if(report_trace){ |
157 | System.out.println("Trace:"); |
158 | for(int i=0; i<turns.size(); i++){ |
159 | System.out.println(turns.get(i) + "\t" + costs.get(i)); |
160 | } |
161 | } |
162 | System.out.println("Total run-time = " + UIMan.comma(groundSec + inferSec) + " sec"); |
163 | System.out.println("Ground time = " + UIMan.comma(groundSec) + " sec"); |
164 | System.out.println("Inference time = " + UIMan.comma(inferSec) + " sec"); |
165 | //System.out.println("Flip rate = " + getFlipRate()); |
166 | System.out.println("Effective #flips in MAP inference = " + UIMan.comma(effectiveSteps)); |
167 | System.out.println("Base memory = " + UIMan.comma(DebugMan.getBaseMem()) + " bytes"); |
168 | System.out.println("Peak memory = " + UIMan.comma(DebugMan.getPeakMem()) + " bytes"); |
169 | System.out.println("============================"); |
170 | } |
171 | } |
172 | |
173 | } |