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