-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathHRTimer.h
More file actions
48 lines (40 loc) · 1.08 KB
/
HRTimer.h
File metadata and controls
48 lines (40 loc) · 1.08 KB
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
#ifndef __JHRTIMER__
#define __JHRTIMER__
#include <stdint.h>
#include <unistd.h>
#include "Utilities.h"
namespace OrderBook {
//-----------------------------------------------------------------------------
class HRTimer {
public:
HRTimer()
: start_(0) {
int64_t start = RDTSC();
usleep(250000);
int64_t end = RDTSC();
int64_t duration = end - start;
uint32_t ticks_sec = duration * 4;
fprintf(stderr, "Ticks/sec: %u. MHz: %f\n", ticks_sec, ticks_sec / 1000000.0f);
hz_ = ticks_sec;
}
inline void start() {
start_ = RDTSC();
}
inline uint64_t stop() {
if (UNLIKELY(start_ == 0)) return 0;
uint64_t nano = ((RDTSC() - start_) * 1000000000) / hz_;
start_ = 0;
return nano;
}
private:
//-----------------------------------------------------------------------------
inline int64_t RDTSC() {
static uint32_t hi, lo;
__asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi));
return ( (uint64_t)lo) | (((uint64_t)hi) << 32);
}
uint64_t start_;
uint64_t hz_;
};
} // namespace
#endif