-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathchronograph.h
More file actions
116 lines (103 loc) · 2.7 KB
/
chronograph.h
File metadata and controls
116 lines (103 loc) · 2.7 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
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
113
114
115
116
/* Author: Samuel Hornus <samuel.hornus@inria.fr>
* Copyright © Inria, 2017
* Licence: Creative Commons CC BY-ND 3.0 license available online at
* http://creativecommons.org/licenses/by-nd/3.0/
*/
#ifndef CHRONOGRAPH_H
#define CHRONOGRAPH_H
#endif // CHRONOGRAPH_H
#include <ctime>
#include <string>
#include <iostream>
#ifdef __APPLE__
#include <mach/mach.h>
#include <mach/mach_time.h>
#elif defined(__linux__)
#include <time.h>
#endif
class Chronograph {
public:
Chronograph(const std::string & s) : acc_time_(0.0), name_(s), running(false) {
#ifdef __APPLE__
mach_timebase_info(&timebase_info_);
#elif defined(__linux__)
clock_getres(CLOCK_MONOTONIC, &time_res);
#endif
}
Chronograph() : acc_time_(0.0), name_(""), running(false) {
#ifdef __APPLE__
mach_timebase_info(&timebase_info_);
#elif defined(__linux__)
clock_getres(CLOCK_MONOTONIC, &time_res);
#endif
}
#ifdef __APPLE__
inline uint64_t now() const {
return mach_absolute_time();
}
#elif defined(__linux__)
inline uint64_t now() const {
clock_gettime(CLOCK_MONOTONIC, &ts);
return static_cast<uint64_t>(ts.tv_sec)*1000000000 + ts.tv_nsec;
}
#endif
void start() {
if( running ) return;
running = true;
absolute_ = now();
}
void stop() {
if( ! running ) return;
acc_time_ += now() - absolute_;
running = false;
}
double elapsed_time() const {
uint64_t t;
if( running ) {
t = acc_time_ + now() - absolute_;
} else {
t = acc_time_;
}
#ifdef __APPLE__
return 1e-9 * (t * timebase_info_.numer / timebase_info_.denom);
#elif defined(__linux__)
return 1e-9 * t;
#endif
}
template< class Output >
void print(Output & o) {
o << name_ << " [" << elapsed_time() << " s.]";
}
void reset() {
acc_time_ = 0;
absolute_ = 0;
running = false;
}
void restart() {
reset();
start();
}
private:
uint64_t acc_time_;
std::string name_; // not a const reference otherwise scoped chronograph crashes
uint64_t absolute_;
bool running;
#ifdef __APPLE__
struct mach_timebase_info timebase_info_;
#elif defined(__linux__)
mutable struct timespec ts;
struct timespec time_res;
#endif
};
template< typename OutputStream >
class ScopedChronograph : public Chronograph {
public:
ScopedChronograph(OutputStream & o) : Chronograph(), out_(o) { Chronograph::start(); }
ScopedChronograph(OutputStream & o, const std::string & s) : Chronograph(s), out_(o) { Chronograph::start(); }
~ScopedChronograph() {
Chronograph::stop();
Chronograph::print(out_);
}
private:
OutputStream & out_;
};