1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html#License
3 //
4 //  ElapsedTimer.java
5 //
6 //  Created by Steven R. Loomis on 11/11/2005.
7 //  Copyright 2005-2012 IBM. All rights reserved.
8 //
9 
10 package com.ibm.icu.dev.util;
11 
12 import java.util.Locale;
13 
14 import com.ibm.icu.text.MessageFormat;
15 import com.ibm.icu.text.NumberFormat;
16 import com.ibm.icu.text.RuleBasedNumberFormat;
17 
18 
19 /**
20  * Simple stopwatch timer.
21  * Usage:   { ElapsedTimer et = new ElapsedTimer();
22  *            do_some_stuff;
23  *            System.out.println("It took " + et + " to do stuff."); }
24  *
25  * Advanced:   { ElapsedTimer et = new ElapsedTimer("Thing2's time: {0}");  // messageformat pattern
26  *            do_thing_2();
27  *            System.out.println(et.toString()); }
28  *
29  * More advanced:  NumberFormat and/or MessageFormat can be provided in the constructor
30  *
31  * @internal CLDR
32  */
33 public final class ElapsedTimer {
34 
35     /**
36      * Convenience method to print the elasped time (in milliseconds)
37      */
elapsedTime(long start, long end)38     public static String elapsedTime(long start, long end) {
39         return diffTime(getFormat(), start, end);
40     }
41 
elapsedTime(long start)42     public static String elapsedTime(long start) {
43         return diffTime(getFormat(), start, System.currentTimeMillis());
44     }
45 
46     // class
47 
48     private long startTime = System.currentTimeMillis();
49     private NumberFormat myDurationFormat = null;
50     private MessageFormat myMsgFormat = null;
51 
ElapsedTimer()52     public ElapsedTimer() {
53     }
54 
ElapsedTimer(MessageFormat aMsgFmt)55     public ElapsedTimer(MessageFormat aMsgFmt) {
56         myMsgFormat = aMsgFmt;
57     }
58 
ElapsedTimer(NumberFormat aNumFmt)59     public ElapsedTimer(NumberFormat aNumFmt) {
60         myDurationFormat = aNumFmt;
61     }
62 
ElapsedTimer(MessageFormat aMsgFmt, NumberFormat aNumFmt)63     public ElapsedTimer(MessageFormat aMsgFmt, NumberFormat aNumFmt) {
64         myMsgFormat = aMsgFmt;
65         myDurationFormat = aNumFmt;
66     }
67 
ElapsedTimer(String pattern)68     public ElapsedTimer(String pattern) {
69         myMsgFormat = new MessageFormat(pattern);
70     }
71 
ElapsedTimer(String pattern, NumberFormat aNumFmt)72     public ElapsedTimer(String pattern, NumberFormat aNumFmt) {
73         myMsgFormat = new MessageFormat(pattern);
74         myDurationFormat = aNumFmt;
75     }
76 
77     /**
78      * @return elapsed time in seconds since object creation
79      */
toString()80     public final String toString() {
81         long endTime = System.currentTimeMillis();
82         String duration = diffTime(myDurationFormat, startTime, endTime);
83         if(myMsgFormat == null) {
84             return duration;
85         } else {
86             return myMsgFormat.format(new Object[] {duration});
87         }
88     }
89 
90     private static NumberFormat gFormat = null;
91 
getFormat()92     private static NumberFormat getFormat() {
93         if(gFormat == null) {
94             gFormat = new RuleBasedNumberFormat(Locale.US,
95                         RuleBasedNumberFormat.DURATION);
96         }
97         return gFormat;
98     }
99 
diffTime(NumberFormat fmt, long start, long end)100     private static String diffTime(NumberFormat fmt, long start, long end) {
101         if(fmt==null) {
102             fmt = getFormat();
103         }
104         synchronized(fmt) {
105             long age = end - start;
106             long diff = age/1000; // millis per second. Workaround ticket:7936 by using whole number seconds.
107             return fmt.format(diff);
108         }
109     }
110 }
111