-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimer.hpp
71 lines (53 loc) · 1.33 KB
/
timer.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#ifndef _TIMER_HPP
#define _TIMER_HPP
#include <sys/time.h>
class Timer
{
struct timeval start;
struct timeval startpause;
double pausecompensate;
public:
Timer () {}
/// Start timing.
void tick ()
{
gettimeofday(&start, 0);
pausecompensate=0;
}
double tuck (const char *msg) const
{
/*struct timeval end;*/
double diff = get_time();
if (msg != 0) {
printf("%s: time=%fs\n",msg,diff);
}
return diff;
}
/// Return time in seconds (excluding paused time)
double get_time () const
{
struct timeval end;
double diff;
double diff_nopause;
gettimeofday(&end, 0);
diff_nopause = (end.tv_sec - start.tv_sec)
+ (end.tv_usec - start.tv_usec) * 0.000001;
diff = diff_nopause - pausecompensate;
return diff;
}
// Pause time tracking
void pause() {
gettimeofday(&startpause, 0);
}
// Continue time tracking
void contin() {
struct timeval endpause;
double diff;
gettimeofday(&endpause, 0);
diff = (endpause.tv_sec - startpause.tv_sec)
+ (endpause.tv_usec - startpause.tv_usec) * 0.000001;
pausecompensate = pausecompensate + diff;
}
};
Timer g_timer;
#endif