xref: /aosp_15_r20/external/cldr/tools/cldr-code/src/main/java/org/unicode/cldr/util/Timer.java (revision 912701f9769bb47905792267661f0baf2b85bed5)
1 package org.unicode.cldr.util;
2 
3 import com.ibm.icu.text.DecimalFormat;
4 import com.ibm.icu.text.MeasureFormat;
5 import com.ibm.icu.text.MeasureFormat.FormatWidth;
6 import com.ibm.icu.text.NumberFormat;
7 import com.ibm.icu.util.Measure;
8 import com.ibm.icu.util.MeasureUnit;
9 import com.ibm.icu.util.ULocale;
10 
11 public final class Timer {
12     private static final double NANOS_PER_SECOND = 1000000000.0d;
13 
14     private long startTime;
15     private long duration;
16 
17     {
start()18         start();
19     }
20 
start()21     public void start() {
22         startTime = System.nanoTime();
23         duration = Long.MIN_VALUE;
24     }
25 
getDuration()26     public long getDuration() {
27         if (duration == Long.MIN_VALUE) {
28             duration = System.nanoTime() - startTime;
29         }
30         return duration;
31     }
32 
getNanoseconds()33     public long getNanoseconds() {
34         return getDuration();
35     }
36 
getSeconds()37     public double getSeconds() {
38         return getDuration() / NANOS_PER_SECOND;
39     }
40 
41     /**
42      * Return nanos
43      *
44      * @return
45      */
stop()46     public long stop() {
47         return getDuration();
48     }
49 
50     @Override
toString()51     public String toString() {
52         return nf.format(getDuration() / NANOS_PER_SECOND) + "s";
53     }
54 
55     /**
56      * @return the duration as a measureformat string
57      */
toMeasureString()58     public String toMeasureString() {
59         double seconds = getSeconds();
60         double minutes = Math.floorDiv((int) seconds, 60);
61         seconds = seconds - (minutes * 60.0);
62 
63         return MeasureFormat.getInstance(ULocale.ENGLISH, FormatWidth.SHORT)
64                 .formatMeasures(
65                         new Measure(seconds, MeasureUnit.SECOND),
66                         new Measure(minutes, MeasureUnit.MINUTE));
67     }
68 
toString(Timer other)69     public String toString(Timer other) {
70         return toString(1L, other.getDuration());
71     }
72 
toString(long iterations)73     public String toString(long iterations) {
74         return nf.format(getDuration() / (NANOS_PER_SECOND * iterations)) + "s";
75     }
76 
toString(long iterations, long other)77     public String toString(long iterations, long other) {
78         return toString(iterations) + "\t(" + pf.format((double) getDuration() / other - 1D) + ")";
79     }
80 
81     private static DecimalFormat nf =
82             (DecimalFormat) NumberFormat.getNumberInstance(ULocale.ENGLISH);
83     private static DecimalFormat pf =
84             (DecimalFormat) NumberFormat.getPercentInstance(ULocale.ENGLISH);
85 
86     static {
87         nf.setMaximumSignificantDigits(3);
88         pf.setMaximumFractionDigits(1);
89         pf.setPositivePrefix("+");
90     }
91 }
92