]> AND Private Git Repository - loba.git/blob - timer.h
Logo AND Algorithmique Numérique Distribuée

Private GIT Repository
Fix comment.
[loba.git] / timer.h
1 #ifndef TIMER_H
2 #define TIMER_H
3
4 #include <sys/time.h>
5 #include <sys/resource.h>
6
7 #ifdef _BSD_SOURCE
8 #  define HAVE_TIMERADD
9 #  define HAVE_TIMERSUB
10 #  define HAVE_TIMERCLEAR
11 #else
12 #  warning _BSD_SOURCE not defined
13 #  undef HAVE_TIMERADD
14 #  undef HAVE_TIMERSUB
15 #  undef HAVE_TIMERCLEAR
16 #endif
17
18 inline
19 struct timeval operator+(const struct timeval& a, const struct timeval& b)
20 {
21     struct timeval result;
22 #ifdef HAVE_TIMERADD
23     timeradd(&a, &b, &result);
24 #else
25     result.tv_sec = a.tv_sec + b.tv_sec;
26     result.tv_usec = a.tv_usec + b.tv_usec;
27     if (result.tv_usec >= 1000000) {
28         ++result.tv_sec;
29         result.tv_usec -= 1000000;
30     }
31 #endif
32     return result;
33 }
34
35 inline
36 struct timeval operator-(const struct timeval& a, const struct timeval& b)
37 {
38     struct timeval result;
39 #ifdef HAVE_TIMERSUB
40     timersub(&a, &b, &result);
41 #else
42     result.tv_sec = a.tv_sec - b.tv_sec;
43     result.tv_usec = a.tv_usec - b.tv_usec;
44     if (result.tv_usec < 0) {
45         --result.tv_sec;
46         result.tv_usec += 1000000;
47     }
48 #endif
49     return result;
50 }
51
52 class timestamp {
53 public:
54     timestamp();
55     ~timestamp();
56     void reset();
57
58     void start();
59     void stop();
60
61     struct timeval tv_duration() const;
62     double duration() const;
63
64 private:
65     struct rusage before;
66     struct rusage after;
67     struct timeval difference;
68
69     static void tv_clear(struct timeval& a);
70     static double timertod(const struct timeval& a);
71 };
72
73 inline
74 timestamp::timestamp()
75 {
76     reset();
77 }
78
79 inline
80 timestamp::~timestamp()
81 {
82 }
83
84 inline
85 void timestamp::reset()
86 {
87     tv_clear(before.ru_utime);
88     tv_clear(before.ru_stime);
89     tv_clear(after.ru_utime);
90     tv_clear(after.ru_stime);
91     tv_clear(difference);
92 }
93
94 inline
95 void timestamp::start()
96 {
97     getrusage(RUSAGE_SELF, &before);
98 }
99
100 inline
101 void timestamp::stop()
102 {
103     getrusage(RUSAGE_SELF, &after);
104     difference = difference + ((after.ru_utime + after.ru_stime) -
105                                (before.ru_utime + before.ru_stime));
106 }
107
108 inline
109 struct timeval timestamp::tv_duration() const
110 {
111     return difference;
112 }
113
114 inline
115 double timestamp::duration() const
116 {
117     return timertod(difference);
118 }
119
120 inline
121 void timestamp::tv_clear(struct timeval& a)
122 {
123 #ifdef HAVE_TIMERCLEAR
124     timerclear(&a);
125 #else
126     tv.sec = tv.usec = 0;
127 #endif
128 }
129
130 inline
131 double timestamp::timertod(const struct timeval& a)
132 {
133     return a.tv_sec + a.tv_usec / 1e6;
134 }
135
136 #endif // !TIMER_H
137
138 // Local variables:
139 // mode: c++
140 // End: