-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimer.h
102 lines (80 loc) · 2.33 KB
/
timer.h
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#ifndef _TIMER_H_
#define _TIMER_H_
#include <stdlib.h>
#include <sys/time.h>
#include <map>
#include "batch_dedup_config.h"
using namespace std;
class Timer {
private:
timeval start_time_;
double duration_; // in milliseconds
bool is_running_;
public:
Timer();
void Start();
// stop and reset timer, return the total duration
double Reset();
// stop the timer and return the last duration
double Stop();
// return the current total duration
double GetDuration();
};
class TimerPool {
private:
static map<string, Timer> timer_map_;
public:
static void Start(const string& timer_name);
static double Stop(const string& timer_name);
static double Reset(const string& time_name);
// stop the timer, print the last duration and total duration
static void Print(const string& timer_name);
static void PrintAll();
};
// At most of the time we only need to measure the time within a single function,
// so we define the following local timer interface. If one want to start/stop timer
// at different functions, he can use TimerPool directly.
#define TIMER_START() \
do { \
string timer_name = __FILE__; \
timer_name += "::"; \
timer_name += __PRETTY_FUNCTION__; \
TimerPool::Start(timer_name); \
} while (0)
#define TIMER_STOP() \
do { \
string timer_name = __FILE__; \
timer_name += "::"; \
timer_name += __PRETTY_FUNCTION__; \
TimerPool::Stop(timer_name); \
} while (0)
#define TIMER_PRINT() \
do { \
string timer_name = __FILE__; \
timer_name += "::"; \
timer_name += __PRETTY_FUNCTION__; \
TimerPool::Print(timer_name); \
} while (0)
#define TIMER_PRINT_ALL() \
do { \
TimerPool::PrintAll(); \
} while (0)
#define MEASURE(statement) \
do { \
string timer_name = __FILE__; \
timer_name += "::"; \
timer_name += #statement; \
TimerPool::Start(timer_name); \
statement; \
TimerPool::Stop(timer_name); \
} while (0)
#define MEASURE_AND_PRINT(statement) \
do { \
string timer_name = __FILE__; \
timer_name += "::"; \
timer_name += #statement; \
TimerPool::Start(timer_name); \
statement; \
TimerPool::Print(timer_name); \
} while (0)
#endif // _TIMER_H_