1 /*-----------------------------------------------------------------------------
2 Name: StopWatch.h
3 Project: xmlBlaster.org
4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
5 Comment: Handling the Client data
6 -----------------------------------------------------------------------------*/
7
8 #ifndef _UTIL_STOPWATCH_H
9 #define _UTIL_STOPWATCH_H
10
11 #include <util/XmlBCfg.h>
12 #if !defined(WINCE)
13 #include <sys/timeb.h>
14 #endif
15 #include <string>
16 #include <util/lexical_cast.h>
17
18
19 namespace org { namespace xmlBlaster {
20 namespace util {
21
22 /**
23 * Measure the elapsed time. <p />
24 * Use this helper class if you want to measure elapsed time in some code
25 * fragment. If you specify (a positive) time in the argument list, it is the
26 * time in milliseconds which the stop watch should wait.
27 * <p>Is not implemented for Windows CE</p>
28 */
29 class Dll_Export StopWatch {
30
31 private:
32 #if defined(WINCE)
33 #else
34 struct timeb *startTime_, *currentTime_;
35 #endif
36 long stopTime_;
37
38 public:
39 StopWatch(long stopTime=-1) {
40 #if defined(WINCE)
41 #else
42 startTime_ = new timeb();
43 currentTime_ = new timeb();
44 restart(stopTime);
45 #endif
46 }
47
48
49 ~StopWatch() {
50 #if defined(WINCE)
51 #else
52 delete startTime_;
53 delete currentTime_;
54 #endif
55 }
56
57
58 /**
59 * Return the elapsed milliseconds since creation or since the last
60 * restart().<p />
61 * @return elapsed Milliseconds since creation or restart()
62 */
63 long elapsed() const {
64 #if defined(WINCE)
65 return 0;
66 #else
67 ftime(currentTime_);
68 double seconds = difftime(currentTime_->time, startTime_->time);
69 double milliSec =
70 (double)currentTime_->millitm - (double)startTime_->millitm;
71 return (long)(1000.0 * seconds + milliSec);
72 #endif //(WINCE)
73 }
74
75
76
77 /**
78 * Returns a nice std::string with elapsed time.
79 * Resets the stop watch.
80 * @return The elapsed time in a nice formatted std::string
81 */
82 std::string nice() {
83 std::string ret = toString();
84 restart();
85 return ret;
86 }
87
88
89 /**
90 * Nicely formatted output containing elapsed time since
91 * Construction or since last restart().
92 * <p />
93 * @return The elapsed time in a nice formatted std::string
94 */
95 std::string toString() const {
96 return std::string("elapsed time: ") + lexical_cast<std::string>(elapsed()) + " milliseconds";
97 }
98
99
100 /**
101 * Reset and start the stop watch for a new measurement cycle.
102 * <p />
103 */
104 void restart(long stopTime=-1) {
105 stopTime_ = stopTime;
106 #if defined(WINCE)
107 #else
108 ftime(startTime_);
109 #endif
110 }
111
112 /**
113 * returns true if the stopwatch is still running, false if it has
114 * stopped (i.e. when the time specified in the constructor of in the
115 * restart method has elapsed). If no stop time was given (nothing was
116 * passed to the constructor or restart), then it always returns true.
117 */
118 bool isRunning() const {
119 if (stopTime_ < 0) return true; // always running
120 return (stopTime_ > elapsed());
121 }
122
123
124 /**
125 * Waits for the amount of milliseconds specified in millisec and then
126 * returns.
127 */
128 void wait(long millisec) {
129 restart(millisec);
130 while (isRunning()) {
131 }
132 }
133 };
134 }}} // namespace
135
136 #endif
syntax highlighted by Code2HTML, v. 0.9.1