-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtiming.h
64 lines (59 loc) · 1.94 KB
/
timing.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
#include <math.h>
#include <mpi.h>
#include <string>
#include <time.h>
#define BILLION 1000000000
class RaiiTimer {
class RaiiIterTimer {
RaiiTimer *timer{nullptr};
public:
RaiiIterTimer(RaiiTimer *timer) : timer(timer) { timer->startIter(); }
~RaiiIterTimer() { timer->stopIter(); }
};
struct timespec start {};
struct timespec istart {};
double total{0}, sumsqr{0}, imin{1e10}, imax{0};
int samples{0};
std::string description{};
public:
RaiiTimer(const std::string &descr) : description(descr) {
clock_gettime(CLOCK_MONOTONIC, &start);
}
RaiiTimer(int id, const char *descr)
: description(std::to_string(id) + ": " + std::string(descr)) {
clock_gettime(CLOCK_MONOTONIC, &start);
}
RaiiIterTimer iterTimer() { return RaiiIterTimer{this}; }
void startIter() { clock_gettime(CLOCK_MONOTONIC, &istart); }
void stopIter() {
struct timespec iend;
clock_gettime(CLOCK_MONOTONIC, &iend);
double startd =
(double)(istart.tv_sec) + ((double)(istart.tv_nsec) / BILLION);
double endd = (double)(iend.tv_sec) + ((double)(iend.tv_nsec) / BILLION);
double elapsed = endd - startd;
total += elapsed;
sumsqr += elapsed * elapsed;
if (elapsed < imin)
imin = elapsed;
if (elapsed > imax)
imax = elapsed;
samples++;
}
~RaiiTimer() {
struct timespec end;
clock_gettime(CLOCK_MONOTONIC, &end);
double startd =
(double)(start.tv_sec) + ((double)(start.tv_nsec) / BILLION);
double endd = (double)(end.tv_sec) + ((double)(end.tv_nsec) / BILLION);
double elapsed = endd - startd;
double mean = total / samples;
double stddev2 = (sumsqr / samples) - (mean * mean);
double error = sqrt(stddev2);
printf(
"%-25s total = %f, iter runtime (ms) = %f +/- %1.9f (%2.1f%%), min / max "
"(ms) = %f / %f\n",
description.c_str(), total, mean * 1000, error * 1000,
error / mean * 100, imin * 1000, imax * 1000);
}
};