ConicBundle
clock.hxx
Go to the documentation of this file.
1 
2 
3 #ifndef CH_TOOLS__CLOCK_HXX
4 #define CH_TOOLS__CLOCK_HXX
5 
14 #include <iostream>
15 #include <iomanip>
16 extern "C"{
17 #include <time.h>
18 }
19 #ifdef __unix
20 extern "C"{
21 #include <sys/resource.h>
22 #include <sys/timeb.h>
23 //we need extern int getrusage(int who, struct rusage *r_usage);
24 }
25 #endif
26 
29 namespace CH_Tools {
30 
36 
46  class Microseconds{
47  bool infinity;
48  long seconds;
49  long microsecs;
50 
51 public:
55 
57  Microseconds(){seconds=0;microsecs=0;infinity=false;}
59  Microseconds(bool infty){seconds=0;microsecs=0;infinity=infty;}
62  {seconds=m.seconds; microsecs=m.microsecs;infinity=m.infinity;}
64  Microseconds(long secs, long msecs=0)
65  {seconds=secs; microsecs=msecs;infinity=false;}
66 
68  Microseconds(int secs, int msecs=0)
69  {seconds=secs; microsecs=msecs;infinity=false;}
71  Microseconds(long hours,long minutes,long secs, long micros)
72  {seconds=hours*3600+minutes*60+secs; microsecs=micros;infinity=false;}
74  Microseconds(int hours,int minutes,int secs, int micros)
75  {seconds=hours*3600+minutes*60+secs; microsecs=micros;infinity=false;}
76 
78 
79 
83 
85  Microseconds& operator=(const Microseconds& m)
86  {seconds=m.seconds; microsecs=m.microsecs; infinity=m.infinity;return *this;}
87 
89  Microseconds& operator+=(const Microseconds& m)
90  {
91  if (m.infinity){
92  infinity=true;
93  return *this;
94  }
95  microsecs+=m.microsecs;
96  seconds+=m.seconds;
97  while (microsecs>1000000){
98  seconds++;
99  microsecs-=1000000;
100  }
101  return *this;
102  }
103 
105  Microseconds& operator-=(const Microseconds& m)
106  {
107  microsecs-=m.microsecs;
108  seconds-=m.seconds;
109  while (microsecs<0) {
110  seconds--; microsecs+=1000000;
111  }
112  return *this;
113  }
114 
116  Microseconds operator-(const Microseconds& m) const
117  {Microseconds s(*this); return s-=m;}
119  Microseconds operator+(const Microseconds& m) const
120  {Microseconds s(*this); return s+=m;}
121 
123  bool operator<(const Microseconds& m) const
124  {
125  if (infinity) return false;
126  if (m.infinity) return true;
127  return ((seconds<m.seconds)||((seconds==m.seconds)&&(microsecs<m.microsecs)));
128  }
129 
131  bool operator>(const Microseconds& m) const
132  {
133  if (infinity&&(!m.infinity)) return true;
134  if (m.infinity) return false;
135  return ((seconds>m.seconds)||((seconds==m.seconds)&&(microsecs>m.microsecs)));}
136 
138  bool operator<=(const Microseconds& m) const
139  {
140  if (infinity&&(!m.infinity)) return false;
141  if (m.infinity) return true;
142  return ((seconds<m.seconds)||((seconds==m.seconds)&&(microsecs<=m.microsecs)));}
143 
145  bool operator>=(const Microseconds& m) const
146  {
147  if (infinity) return true;
148  if (m.infinity) return false;
149  return ((seconds>m.seconds)||((seconds==m.seconds)&&(microsecs>=m.microsecs)));}
150 
152  bool operator==(const Microseconds& m) const
153  {
154  if ((infinity&&(!m.infinity))||(m.infinity)) return false;
155  return ((seconds==m.seconds)&&(microsecs==m.microsecs));}
156 
158 
162 
164  void set_infinity(bool infty){infinity=infty;}
166  bool get_infinity() const {return infinity;}
167 
169  operator double() const
170  { return double(seconds)+double(microsecs)/1000000.; }
171 
173  void hhmmss(long& hours,long& minutes,long& secs) const
174  {
175  long s=seconds;
176  if (microsecs>=500000) s++;
177  hours=s/3600;
178  minutes=(s%3600)/60;
179  secs=(s%60);
180  }
181 
183  void hhmmssdd(long& hours,long& minutes,long& secs,long& hund) const
184  {
185  hund=(microsecs+5000)/10000;
186  long s=seconds;
187  if (hund==100) {s++;hund=0;}
188  hours=s/3600;
189  minutes=(s%3600)/60;
190  secs=(s%60);
191  }
192 
194  long roundsecs() const
195  {
196  if (microsecs>=500000) return seconds+1;
197  return seconds;
198  }
199 
201  long roundhundredths() const
202  {
203  long hund=(microsecs+5000)/10000;
204  hund+=seconds*100;
205  return hund;
206  }
207 
209 
213 
215  friend std::ostream& operator<<(std::ostream& out,const Microseconds& m)
216  {if (m.infinity) return out<<"-1.000000";
217  out<<m.seconds<<".";out.fill('0');out.width(6);out<<m.microsecs;
218  out.fill(' ');return out;}
219 
220 
222  friend std::istream& operator>>(std::istream& in,Microseconds& m)
223  {
224  char c; m.infinity=false;
225  in>>m.seconds>>c>>m.microsecs;
226  if (m.seconds<0) {m.seconds=0; m.infinity=true;}
227  return in;
228  }
229 
231  friend void print_time(std::ostream& out,
232  const Microseconds& m,
233  int secondsonly=0
234  )
235  {
236  long hours,minutes,seconds,hunds;
237  out.fill('0');
238  if (secondsonly){
239  m.hhmmss(hours,minutes,seconds);
240  out.width(2);out<<hours;
241  out<<":";out.width(2);out<<minutes;
242  out<<":";out.width(2);out<<seconds;
243  }
244  else {
245  m.hhmmssdd(hours,minutes,seconds,hunds);
246  out.width(2);out<<hours;
247  out<<":";out.width(2);out<<minutes;
248  out<<":";out.width(2);out<<seconds;
249  out<<".";out.width(2);out<<hunds;
250  }
251  out.fill(' ');
252  }
253 
255 
256 };
257 
258 
261  {
262 #ifdef __unix
263  struct timeb ts;
264  ftime(&ts);
265  return Microseconds(long(ts.time),long(ts.millitm)*1000);
266 #else
267  return Microseconds();
268 #endif
269  }
270 
271 
282 class Clock
283 {
284  private:
288 #ifdef __unix
289 #else
290  long l_start;
291 #endif
292 
293  public:
295  void start(){
296 #ifdef __unix
297  struct rusage r_usage;
298  getrusage(RUSAGE_SELF,&r_usage);
299  t_start=Microseconds(r_usage.ru_utime.tv_sec,r_usage.ru_utime.tv_usec);
300  offset=Microseconds(0,0);
301  wall_start=wall_clock();
302 #else
303  t_start=Microseconds(static_cast<int>(::time(0)),0);
304 #endif
305  }
306 
308  Clock(){start();}
310  ~Clock(){}
311 
314  offset=offs;
315  }
316 
319 #ifdef __unix
320  struct rusage r_usage;
321  getrusage(RUSAGE_SELF,&r_usage);
322  Microseconds elapsed(r_usage.ru_utime.tv_sec,r_usage.ru_utime.tv_usec);
323 #else
324  Microseconds elapsed(static_cast<int>(::time(0)),0);
325 #endif
326  elapsed-=t_start;
327  elapsed+=offset;
328  return elapsed;
329  }
330 
333  return wall_clock()-wall_start;
334  }
335 
337  void elapsed_time(std::ostream& out) const
338  {
339  time_t current=::time(0);
340  struct tm *loctime;
341  Microseconds spent=time();
342  out<<"elapsed time: ";
343  print_time(out,wall_clock()-wall_start);
344  out<<" user: ";
345  print_time(out,spent);
346  loctime=localtime(&current);
347  out<<" ---- "<<asctime(loctime)<<std::flush;
348  }
349 
351  friend inline std::ostream& operator<<(std::ostream& out,const Clock& cl)
352  {
353  print_time(out,cl.time());
354  return out;
355  }
356 };
357 
359 
360 }
361 
362 #endif
363 
Some convenient inline tools like a clock for timing, a random number generator, a heapsort template ...
Definition: clock.hxx:29
void start()
read current time, all further time measurements will be in relation to this time ...
Definition: clock.hxx:295
bool get_infinity() const
if true, value should be regarded as infinity
Definition: clock.hxx:166
Microseconds(int secs, int msecs=0)
specify directly seconds and microseconds (in [0,10^6], no range check!)
Definition: clock.hxx:68
Microseconds wall_start
wall clock time on initilazation
Definition: clock.hxx:287
long seconds
counts time portion in seconds
Definition: clock.hxx:48
Microseconds(long secs, long msecs=0)
specify directly seconds and microseconds (in [0,10^6], no range check!)
Definition: clock.hxx:64
Microseconds(const Microseconds &m)
copy constructor
Definition: clock.hxx:61
Microseconds wall_time() const
returns the Microseconds passed on the wall clock sind initialization
Definition: clock.hxx:332
void hhmmssdd(long &hours, long &minutes, long &secs, long &hund) const
convert and store the value of (*this) to hours, minutes, seconds, hundredths
Definition: clock.hxx:183
void hhmmss(long &hours, long &minutes, long &secs) const
convert and store the value of (*this) to hours, minutes, seconds
Definition: clock.hxx:173
long roundsecs() const
round the value to seconds
Definition: clock.hxx:194
long l_start
if not unix, use long values instead
Definition: clock.hxx:290
allows measuring time difference to its initialization time in Microseconds
Definition: clock.hxx:282
Microseconds()
default constructor, value 0
Definition: clock.hxx:57
extra long integer number for expressing and computing time measurements in microseconds.
Definition: clock.hxx:46
Microseconds wall_clock(void)
returns the wall clock time since Epoch
Definition: clock.hxx:260
friend void print_time(std::ostream &out, const Microseconds &m, int secondsonly=0)
print Microseconds in the format "hh:mm:ss.dd" or "hh:mm:ss"
Definition: clock.hxx:231
Microseconds time() const
return time elapsed since last call to start() in Microseconds (possibly adding an optional offset) ...
Definition: clock.hxx:318
long roundhundredths() const
round the value to hundredths
Definition: clock.hxx:201
Clock()
calls start()
Definition: clock.hxx:308
friend std::istream & operator>>(std::istream &in, Microseconds &m)
input in the format "seconds.microseconds" , seconds<0 is regarded as infinity
Definition: clock.hxx:222
Microseconds(long hours, long minutes, long secs, long micros)
convert hours, minutes, secs, micros to Microseconds (no range check!)
Definition: clock.hxx:71
bool infinity
if true, the value is regarded as infinity
Definition: clock.hxx:47
Microseconds offset
use specified offset
Definition: clock.hxx:286
friend std::ostream & operator<<(std::ostream &out, const Microseconds &m)
output in the format "seconds.microseconds" or "-1.000000" for infinity
Definition: clock.hxx:215
long microsecs
number in [0,10^6] counting the remaining microseconds without seconds
Definition: clock.hxx:49
~Clock()
nothing particular
Definition: clock.hxx:310
void set_infinity(bool infty)
use true to regard value as infinity
Definition: clock.hxx:164
void elapsed_time(std::ostream &out) const
call time() and print the result in format "hh:mm:ss", togehter with current date and time...
Definition: clock.hxx:337
Microseconds(bool infty)
constructor for setting value to 0 or infinity
Definition: clock.hxx:59
friend std::ostream & operator<<(std::ostream &out, const Clock &cl)
call cl.time() and print the result in format "hh:mm:ss" to out
Definition: clock.hxx:351
Microseconds t_start
user time value on intialzation
Definition: clock.hxx:285
Microseconds(int hours, int minutes, int secs, int micros)
convert hours, minutes, secs, micros to Microseconds (no range check!)
Definition: clock.hxx:74
void set_offset(Microseconds offs)
allows to specify an offset, that will furtheron be added to all time measurements ...
Definition: clock.hxx:313