001: //
002: // ElapsedTimer.java
003: //
004: // Created by Steven R. Loomis on 11/11/2005.
005: // Copyright 2005-2006 IBM. All rights reserved.
006: //
007:
008: package com.ibm.icu.dev.test.util;
009:
010: import java.util.*;
011: import com.ibm.icu.text.NumberFormat;
012: import com.ibm.icu.text.RuleBasedNumberFormat;
013: import com.ibm.icu.text.MessageFormat;
014:
015: /**
016: * Simple stopwatch timer.
017: * Usage: { ElapsedTimer et = new ElapsedTimer();
018: * do_some_stuff;
019: * System.out.println("It took " + et + " to do stuff."); }
020: *
021: * Advanced: { ElapsedTimer et = new ElapsedTimer("Thing2's time: {0}"); // messageformat pattern
022: * do_thing_2();
023: * System.out.println(et.toString()); }
024: *
025: * More advanced: NumberFormat and/or MessageFormat can be provided in the constructor
026: */
027: public final class ElapsedTimer {
028:
029: /**
030: * Convenience method to print the elasped time (in milliseconds)
031: */
032: public static String elapsedTime(long start, long end) {
033: return diffTime(getFormat(), start, end);
034: }
035:
036: public static String elapsedTime(long start) {
037: return diffTime(getFormat(), start, System.currentTimeMillis());
038: }
039:
040: // class
041:
042: private long startTime = System.currentTimeMillis();
043: private NumberFormat myDurationFormat = null;
044: private MessageFormat myMsgFormat = null;
045:
046: public ElapsedTimer() {
047: }
048:
049: public ElapsedTimer(MessageFormat aMsgFmt) {
050: myMsgFormat = aMsgFmt;
051: }
052:
053: public ElapsedTimer(NumberFormat aNumFmt) {
054: myDurationFormat = aNumFmt;
055: }
056:
057: public ElapsedTimer(MessageFormat aMsgFmt, NumberFormat aNumFmt) {
058: myMsgFormat = aMsgFmt;
059: myDurationFormat = aNumFmt;
060: }
061:
062: public ElapsedTimer(String pattern) {
063: myMsgFormat = new MessageFormat(pattern);
064: }
065:
066: public ElapsedTimer(String pattern, NumberFormat aNumFmt) {
067: myMsgFormat = new MessageFormat(pattern);
068: myDurationFormat = aNumFmt;
069: }
070:
071: /**
072: * @return elapsed time in seconds since object creation
073: */
074: public final String toString() {
075: long endTime = System.currentTimeMillis();
076: String duration = diffTime(myDurationFormat, startTime, endTime);
077: if (myMsgFormat == null) {
078: return duration;
079: } else {
080: return myMsgFormat.format(new Object[] { duration });
081: }
082: }
083:
084: private static NumberFormat gFormat = null;
085:
086: private static NumberFormat getFormat() {
087: if (gFormat == null) {
088: gFormat = new RuleBasedNumberFormat(Locale.US,
089: RuleBasedNumberFormat.DURATION);
090: }
091: return gFormat;
092: }
093:
094: private static String diffTime(NumberFormat fmt, long start,
095: long end) {
096: if (fmt == null) {
097: fmt = getFormat();
098: }
099: synchronized (fmt) {
100: long age = end - start;
101: double diff = (double) age;
102: diff = diff / (double) 1000.0; // millis per second
103: return fmt.format(diff);
104: }
105: }
106: }
|