11 * Use time() call to keep track of elapsed time between creation and
12 * destruction. If verbose is true, Timer will print a message showing
13 * elapsed time to the given output stream upon destruction.
17 Timer(ostream& out = cout, const char *msg = "", bool verbose = true) :
18 _t(time(0)), _out(out), _msg(msg), _verbose(verbose) { }
20 /// Optionally print message
22 if(_verbose) write(_out);
25 /// Return elapsed time since Timer object was created
26 time_t elapsed() const {
30 void write(ostream& out) {
31 time_t passed = elapsed();
32 // Print the message supplied at construction time followed
33 // by time elapsed formatted HH:MM:SS
34 unsigned int hours = (passed / 60) / 60;
35 unsigned int minutes = (passed / 60) % 60;
36 unsigned int seconds = (passed % 60);
37 out << _msg << setfill ('0') << setw (2) << hours << ":"
38 << setfill ('0') << setw (2) << minutes << ":"
39 << setfill ('0') << setw (2) << seconds << endl;
49 static inline void logTime(std::ostream& os, bool nl = true) {
53 current = localtime(&now);
54 os << setfill('0') << setw(2)
55 << current->tm_hour << ":"
56 << setfill('0') << setw(2)
57 << current->tm_min << ":"
58 << setfill('0') << setw(2)
60 if(nl) os << std::endl;