2 * Copyright 2001 Sun Microsystems, Inc.
4 * See the file "license.terms" for information on usage and
5 * redistribution of this file, and for a DISCLAIMER OF ALL
8 package com.sun.speech.freetts.util;
10 import java.text.DecimalFormat;
13 * Keeps track of execution times.
16 private final static DecimalFormat timeFormatter
17 = new DecimalFormat("###0.0000");
18 private final static DecimalFormat percentFormatter
19 = new DecimalFormat("###0.00%");
21 private long startTime;
25 private long minTime = Long.MAX_VALUE;
26 private long maxTime = 0L;
27 private boolean notReliable; // if true, timing is not reliable
32 * @param name the name of the timer
34 public Timer(String name) {
40 * Resets the timer as if it has never run before.
46 minTime = Long.MAX_VALUE;
52 * Starts the timer running.
55 if (startTime != 0L) {
57 // throw new IllegalStateException("timer stutter start " + name);
59 startTime = System.currentTimeMillis();
64 * Returns the current time.
66 * @return the current time
68 public long getCurrentTime() {
75 * @param verbose if <code>true</code>, print out details from
76 * this run; otherwise, don't print the details
78 public void stop(boolean verbose) {
79 if (startTime == 0L) {
82 curTime = System.currentTimeMillis() - startTime;
84 if (curTime > maxTime) {
87 if (curTime < minTime) {
105 * Formats times into a standard format.
107 * @param time the time (in milliseconds) to be formatted
109 * @return a string representation of the time.
111 private String fmtTime(long time) {
112 return fmtTime(time/1000.0);
116 * Formats times into a standard format.
118 * @param time the time (in seconds) to be formatted
120 * @return a string representation of the time.
122 private String fmtTime(double time) {
123 return Utilities.pad(timeFormatter.format(time) + "s", 10);
127 * Shows detailed timing stats. If overall is non-zero, it represents
128 * the overall processing time and a percentage of overall
129 * time for this timer should be calculated and displayed.
131 * @param overall the overall processing time in milliseconds or 0.
133 public void showTimesLong(long overall) {
134 System.out.println(" Timer: " + name);
135 System.out.println(" Count: " + count);
138 System.out.println(" Not reliable");
141 System.out.println(" Cur Time: " + fmtTime(curTime));
142 } else if (count > 1) {
143 System.out.println(" Min Time: " + fmtTime(minTime));
144 System.out.println(" Max Time: " + fmtTime(maxTime));
145 System.out.println(" Avg Time: "
146 + fmtTime(sum / count / 1000.0));
147 System.out.println(" Tot Time: " + fmtTime(sum /1000.0));
149 System.out.println(" Percent: "
150 + percentFormatter.format(sum / overall));
154 System.out.println();
158 * Shows the timing stats title.
160 * @param title shows the title and column headings for the time
163 public static void showTimesShortTitle(String title) {
165 "# ----------------------------- " + title +
166 "----------------------------------------------------------- ";
167 System.out.println(Utilities.pad(titleBar, 78));
168 System.out.print(Utilities.pad("# Name", 15) + " ");
169 System.out.print(Utilities.pad("Count", 6));
170 System.out.print(Utilities.pad("CurTime", 10));
171 System.out.print(Utilities.pad("MinTime", 10));
172 System.out.print(Utilities.pad("MaxTime", 10));
173 System.out.print(Utilities.pad("AvgTime", 10));
174 System.out.print(Utilities.pad("TotTime", 10));
175 System.out.print(Utilities.pad("% Total", 8));
176 System.out.println();
179 * Shows brief timing stats. If overall is non-zero, it represents
180 * the overall processing time and a percentage of overall
181 * time for this timer should be calculated and displayed.
183 * @param overall the overall processing time in milliseconds or 0.
185 public void showTimesShort(long overall) {
186 double avgTime = 0.0;
187 double overallPercent = 0;
200 avgTime = sum / count / 1000.0;
204 overallPercent = sum / overall;
208 System.out.print(Utilities.pad(name, 15) + " ");
209 System.out.println("Not reliable.");
211 System.out.print(Utilities.pad(name, 15) + " ");
212 System.out.print(Utilities.pad("" + count, 6));
213 System.out.print(fmtTime(curTime));
214 System.out.print(fmtTime(minTime));
215 System.out.print(fmtTime(maxTime));
216 System.out.print(fmtTime(avgTime));
217 System.out.print(fmtTime(sum / 1000.0));
218 System.out.print(percentFormatter.format(overallPercent));
219 System.out.println();
224 * Shows timing stats. If overall is non-zero, it represents
225 * the overall processing time and a percentage of overall
226 * time for this timer should be calculated and displayed.
228 * @param overall the overall processing time in milliseconds or 0.
230 public void showTimes(long overall) {
231 showTimesShort(overall);
235 * Shows timing stats. No overall percentage is shown with this
238 public void showTimes() {
244 * The main program for performing simple tests. Creates a timer
245 * uses it and shows its output.
247 * @param args program arguments (not used)
249 public static void main(String[] args) {
250 Timer timer = new Timer("testTimer");
251 Timer overallTimer = new Timer("overall");
254 overallTimer.start();
256 for (int i = 0; i < 5; i++) {
259 Thread.sleep(i * 1000L);
260 } catch (InterruptedException e) {
265 timer.showTimes(overallTimer.getCurrentTime());
266 overallTimer.showTimes();