TexGen
Timer.h
Go to the documentation of this file.
1#ifndef TIMER_H
2#define TIMER_H
3
4#include <ctime>
5#include <iostream>
6#include <iomanip>
7
8namespace TexGen
9{
12 {
13 friend std::ostream& operator<<(std::ostream& os, CTimer& t);
14
15 private:
16 bool running;
17 clock_t start_clock;
18 time_t start_time;
19 double acc_time;
20
25 double elapsed_time();
26
27 public:
28 // 'running' is initially false. A timer needs to be explicitly started
29 // using 'start' or 'restart'
30 CTimer() : running(false), start_clock(0), start_time(0), acc_time(0) { }
31
34 void start(const char* msg = 0);
36 void restart(const char* msg = 0);
38 void stop(const char* msg = 0);
43 void check(const char* msg = 0);
44
45 }; // class timer
46
47 inline double CTimer::elapsed_time()
48 {
49 time_t acc_sec = time(0) - start_time;
50 if (acc_sec < 3600)
51 return (clock() - start_clock) / (1.0 * CLOCKS_PER_SEC);
52 else
53 return (1.0 * acc_sec);
54
55 } // timer::elapsed_time
56
57 inline void CTimer::start(const char* msg)
58 {
59 // Print an optional message, something like "Starting timer t";
60 if (msg) TGLOG(msg);
61
62 // Return immediately if the timer is already running
63 if (running) return;
64
65 // Set timer status to running and set the start time
66 running = true;
67 start_clock = clock();
68 start_time = time(0);
69
70 } // timer::start
71
72
73 inline void CTimer::restart(const char* msg)
74 {
75 // Print an optional message, something like "Restarting timer t";
76 if (msg) TGLOG(msg);
77
78 // Set timer status to running, reset accumulated time, and set start time
79 running = true;
80 acc_time = 0;
81 start_clock = clock();
82 start_time = time(0);
83
84 } // timer::restart
85
86 inline void CTimer::stop(const char* msg)
87 {
88 // Print an optional message, something like "Stopping timer t";
89 if (msg) TGLOG(msg);
90
91 // Compute accumulated running time and set timer status to not running
93 running = false;
94 } // timer::stop
95
96 inline void CTimer::check(const char* msg)
97 {
98 std::stringstream String;
99 // Print an optional message, something like "Checking timer t";
100 if (msg) String << msg << " : ";
101
102 String << "Elapsed time [" << std::setiosflags(std::ios::fixed)
103 << std::setprecision(2)
104 << acc_time + (running ? elapsed_time() : 0) << "] seconds\n";
105
106 TGLOG(String.str());
107
108 } // timer::check
109
110 inline std::ostream& operator<<(std::ostream& os, CTimer& t)
111 {
112 os << std::setprecision(2) << std::setiosflags(std::ios::fixed)
113 << t.acc_time + (t.running ? t.elapsed_time() : 0);
114 return os;
115 }
116
117 //===========================================================================
118}// namespace TexGen;
119
120#endif // TIMER_H
121
#define TGLOG(MESSAGE)
Definition: Logger.h:36
#define CLASS_DECLSPEC
Definition: Misc.h:35
Class used to meaure the amount of time it takes to perform a certain task.
Definition: Timer.h:12
void stop(const char *msg=0)
Stop the timer and print an optional message.
Definition: Timer.h:86
double acc_time
Definition: Timer.h:19
double elapsed_time()
Definition: Timer.h:47
time_t start_time
Definition: Timer.h:18
void restart(const char *msg=0)
Turn the timer off and start it again from 0. Print an optional message.
Definition: Timer.h:73
bool running
Definition: Timer.h:16
clock_t start_clock
Definition: Timer.h:17
void check(const char *msg=0)
Definition: Timer.h:96
void start(const char *msg=0)
Definition: Timer.h:57
Namespace containing a series of customised math operations not found in the standard c++ library.
ostream & operator<<(ostream &output, CMatrix &Matrix)
Definition: Matrix.h:455