timer.hpp
1: #ifndef TIMER_HPP
2: #define TIMER_HPP
3: /*------------------------------------------------------------------------------
4:
5: Author: Andy Rushton
6: Copyright: (c) Andy Rushton, 2004
7: License: BSD License, see ../docs/license.html
8:
9: A CPU timer encapsulated as a class. Measures the CPU time used since its
10: construction and allows this cumulative time to be reported at any time.
11:
12: ------------------------------------------------------------------------------*/
13: #include "os_fixes.hpp"
14: #include <time.h>
15: #include <string>
16: #include "textio.hpp"
17:
18: class timer
19: {
20: private:
21: clock_t m_clock;
22: time_t m_time;
23: public:
24: // constructor resets the timer to zero
25: timer(void);
26: ~timer(void);
27:
28: // reset the timer to zero without destroying it
29: void reset(void);
30:
31: // get the elapsed time in seconds, expressed as a float
32: float elapsed(void) const;
33: // get the CPU time in seconds, expressed as a float
34: float cpu(void) const;
35:
36: // get a printable string representing the elapsed time and CPU time
37: std::string text(void) const;
38: // print the elapsed time and CPU time using the same representation as above
39: friend otext& operator << (otext&, const timer&);
40: };
41:
42: // redefine friends for gcc v4.1
43: otext& operator << (otext&, const timer&);
44:
45: #endif