-
Notifications
You must be signed in to change notification settings - Fork 3
/
timing.h
82 lines (71 loc) · 1.79 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. See the enclosed file LICENSE for a copy or if
* that was not distributed with this file, You can obtain one at
* http://mozilla.org/MPL/2.0/.
*
* Copyright 2017 Max H. Gerlach
*
* */
/*
* timing.h
*
* Created on: Mar 28, 2013
* Author: gerlach
*/
#ifndef TIMING_H_
#define TIMING_H_
#ifdef TIMING
#include <vector>
#include <string>
#include <map>
#include <iostream>
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wconversion"
#pragma GCC diagnostic ignored "-Wshadow"
#include "boost/timer/timer.hpp"
#pragma GCC diagnostic pop
class Timing {
public:
Timing() : insertOrder(), timers() {
}
//on destruction print timing summary
~Timing() {
std::cout << "\nTimings:\n";
for (const auto& timerName : insertOrder) {
std::cout << timerName << ": " << timers[timerName].format();
}
}
//start or resume a timer:
void start(const std::string& timerKey) {
if (timers.count(timerKey) == 0) {
insertOrder.push_back(timerKey);
timers[timerKey] = boost::timer::cpu_timer();
}
timers[timerKey].resume();
}
void stop(const std::string& timerKey) {
timers.at(timerKey).stop();
}
private:
std::vector<std::string> insertOrder;
std::map<std::string,boost::timer::cpu_timer> timers;
};
#else
//version that does nothing:
#include <string>
class Timing {
public:
Timing() {
}
~Timing() {
}
void start(const std::string& timerKey) {
(void)timerKey;
}
void stop(const std::string& timerKey) {
(void)timerKey;
}
};
#endif //TIMING
extern Timing timing; //one global timing object, defined in timing.cpp
#endif /* TIMING_H_ */