-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimer.hpp
112 lines (81 loc) · 2.18 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
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
103
104
105
106
107
108
109
110
111
112
#ifndef TIMING_HPP
#define TIMING_HPP
#include <chrono>
template<class Action, class Resolution = std::chrono::milliseconds,
class Clock = std::chrono::high_resolution_clock>
static double with_time(const Action& action) {
typename Clock::time_point start = Clock::now();
action();
typename Clock::time_point stop = Clock::now();
std::chrono::duration<double> res = stop - start;
return res.count();
}
#include <thread>
#include <vector>
#include <atomic>
struct event {
using clock_type = std::chrono::high_resolution_clock;
using time_type = clock_type::time_point;
using id_type = const char*;
id_type id;
time_type time;
std::thread::id thread;
enum { BEGIN, END } kind;
// named constructors
static inline event begin(id_type id) {
return {id, clock_type::now(), std::this_thread::get_id(), BEGIN};
}
static inline event end(id_type id) {
return {id, clock_type::now(), std::this_thread::get_id(), END};
}
};
class timeline {
std::vector<event> events;
std::atomic<std::size_t> size;
public:
timeline(std::size_t size): events(size), size(0) { };
// add event, return true on success, false otherwise. thread-safe.
bool add(event ev) {
// atomic
const std::size_t index = size++;
if(index >= events.size()) {
return false;
}
events[index] = ev;
return true;
}
// clear timeline. *not* thread-safe
void clear() {
const std::size_t max = size;
events.resize(std::max(max, events.size()));
size = 0;
}
// iterators
const event* begin() const { return events.data(); }
const event* end() const { return events.data() + size; }
static timeline instance;
};
timeline timeline::instance(100000);
struct timer {
const event::id_type id;
timeline& tl;
timer(event::id_type id, timeline& tl=timeline::instance): id(id), tl(tl) {
tl.add(event::begin(id));
}
~timer() { tl.add(event::end(id)); }
};
template<class T>
struct tree: T {
using T::T;
std::vector<tree> children;
};
struct node {
const char* id;
event::time_type begin, end;
};
// static tree<node> process(const timeline& self) {
// for(const event& ev: self) {
// // TODO
// }
// }
#endif