15 // //timezone tz; |
15 // //timezone tz; |
16 // gettimeofday(&tv, 0); |
16 // gettimeofday(&tv, 0); |
17 // return double(tv.tv_sec)+double(tv.tv_usec)/1000000.0; |
17 // return double(tv.tv_sec)+double(tv.tv_usec)/1000000.0; |
18 // } |
18 // } |
19 |
19 |
|
20 /// Class to store (cpu)time instances. |
|
21 |
|
22 /// This class stores five time values. |
|
23 /// - a real time |
|
24 /// - a user cpu time |
|
25 /// - a system cpu time |
|
26 /// - a user cpu time of children |
|
27 /// - a system cpu time of children |
|
28 /// |
|
29 /// TimeStamp's can be added to or substracted from each other and |
|
30 /// they can be push to a stream. |
|
31 |
20 class TimeStamp |
32 class TimeStamp |
21 { |
33 { |
22 tms ts; |
34 tms ts; |
23 double real_time; |
35 double real_time; |
24 |
36 |
25 public: |
37 public: |
26 |
38 |
27 tms &getTms() {return ts;} |
39 tms &getTms() {return ts;} |
28 const tms &getTms() const {return ts;} |
40 const tms &getTms() const {return ts;} |
29 double getRealTime() const {return real_time;} |
41 double getRealTime() const {return real_time;} |
|
42 ///Read the current time values of the process. |
30 void stamp() |
43 void stamp() |
31 { |
44 { |
32 timeval tv; |
45 timeval tv; |
33 times(&ts); |
46 times(&ts); |
34 gettimeofday(&tv, 0);real_time=tv.tv_sec+double(tv.tv_usec)/1e6; |
47 gettimeofday(&tv, 0);real_time=tv.tv_sec+double(tv.tv_usec)/1e6; |
35 } |
48 } |
36 |
49 |
|
50 /// Constructor initializing with zero. |
37 TimeStamp() |
51 TimeStamp() |
38 { ts.tms_utime=ts.tms_stime=ts.tms_cutime=ts.tms_cstime=0; real_time=0;} |
52 { ts.tms_utime=ts.tms_stime=ts.tms_cutime=ts.tms_cstime=0; real_time=0;} |
39 |
53 ///Constructor initializing with the current time values of the process. |
40 TimeStamp(void *) { stamp();} |
54 TimeStamp(void *) { stamp();} |
41 |
55 |
42 TimeStamp &operator+=(const TimeStamp &b) |
56 TimeStamp &operator+=(const TimeStamp &b) |
43 { |
57 { |
44 ts.tms_utime+=b.ts.tms_utime; |
58 ts.tms_utime+=b.ts.tms_utime; |
92 { |
107 { |
93 return double(ts.tms_cstime)/sysconf(_SC_CLK_TCK); |
108 return double(ts.tms_cstime)/sysconf(_SC_CLK_TCK); |
94 } |
109 } |
95 }; |
110 }; |
96 |
111 |
|
112 ///Class measuring the cpu time and real time usage of the process. |
97 class Timer |
113 class Timer |
98 { |
114 { |
99 TimeStamp start_time; |
115 TimeStamp start_time; |
100 |
116 |
101 void _reset() {start_time.stamp();} |
117 void _reset() {start_time.stamp();} |
102 |
118 |
103 public: |
119 public: |
|
120 ///Constructor. It starts with zero time counters. |
104 Timer() {_reset();} |
121 Timer() {_reset();} |
105 |
122 |
|
123 ///Computes the ellapsed time. |
|
124 |
|
125 ///This conversion computes the ellapsed time |
|
126 ///since the construction of \c t or since |
|
127 ///the last \c t.reset(). |
106 operator TimeStamp () |
128 operator TimeStamp () |
107 { |
129 { |
108 TimeStamp t; |
130 TimeStamp t; |
109 t.stamp(); |
131 t.stamp(); |
110 return t-start_time; |
132 return t-start_time; |
111 } |
133 } |
112 |
134 |
|
135 ///Resets the time counters. |
113 TimeStamp reset() |
136 TimeStamp reset() |
114 { |
137 { |
115 TimeStamp t(start_time); |
138 TimeStamp t(start_time); |
116 _reset(); |
139 _reset(); |
117 return start_time-t; |
140 return start_time-t; |
118 } |
141 } |
119 }; |
142 }; |
120 |
143 |
|
144 ///Prints the time counters. |
121 inline std::ostream& operator<<(std::ostream& os,const TimeStamp &t) |
145 inline std::ostream& operator<<(std::ostream& os,const TimeStamp &t) |
122 { |
146 { |
123 long cls = sysconf(_SC_CLK_TCK); |
147 long cls = sysconf(_SC_CLK_TCK); |
124 os << "u: " << double(t.getTms().tms_utime)/cls << |
148 os << "u: " << double(t.getTms().tms_utime)/cls << |
125 "s, s: " << double(t.getTms().tms_stime)/cls << |
149 "s, s: " << double(t.getTms().tms_stime)/cls << |