LatticeHashForest
Loading...
Searching...
No Matches
profiling.hpp
Go to the documentation of this file.
1
6#ifndef LHF_PROFILING_H
7#define LHF_PROFILING_H
8
9#include <cassert>
10#include <chrono>
11#include <cstdint>
12#include <iostream>
13#include <map>
14#include <sstream>
15#include <string>
16
17namespace lhf {
18
23 using Count = uint64_t;
24 using String = std::string;
25 using TimePoint = std::chrono::steady_clock::time_point;
26 template <typename K, typename V> using Map = std::map<K, V>;
27
28 struct Duration {
29 bool started = false;
32 long double duration = 0;
33
34 long double get_curr_duration_ms() {
35 return (std::chrono::duration_cast<std::chrono::microseconds>(t2.time_since_epoch())
36 .count() -
37 std::chrono::duration_cast<std::chrono::microseconds>(t1.time_since_epoch())
38 .count()) /
39 1000.0;
40 }
41
42 long double get_cumul_duration_ms() { return duration; }
43 };
44
47
48 // Timer Functions
49
51 if (timers.count(s) == 0) {
52 timers[s] = Duration{};
53 }
54
55 return timers[s];
56 }
57
58 void timer_start(const String &s) {
59 auto &d = get_timer(s);
60 assert(!d.started && "timer already started");
61 d.started = true;
62 d.t1 = std::chrono::steady_clock::now();
63 }
64
65 void timer_end(const String &s) {
66 auto &d = get_timer(s);
67 assert(d.started && "timer already stopped");
68 d.started = false;
69 d.t2 = std::chrono::steady_clock::now();
70 d.duration += d.get_curr_duration_ms();
71 }
72
73 // Counter Functions
74
76 if (counters.count(s) == 0) {
77 counters[s] = 0;
78 }
79
80 return counters[s];
81 }
82
83 void inc_counter(const String &s) { get_counter(s)++; }
84
85 // dump
86
87 String dump() const {
88 using namespace std;
89 stringstream s;
90
91 if (counters.size() < 1 && timers.size() < 1) {
92 s << endl << "Profiler: No statistics generated" << endl;
93 return s.str();
94 }
95
96 s << endl << "Profiler Statistics:" << endl;
97 for (auto k : counters) {
98 s << " "
99 << "'" << k.first << "'"
100 << ": " << k.second << endl;
101 }
102 for (auto k : timers) {
103 s << " "
104 << "'" << k.first << "'"
105 << ": " << k.second.get_cumul_duration_ms() << " ms" << endl;
106 }
107
108 return s.str();
109 }
110};
111
116 const std::string key;
118
121
123};
124
142#ifdef LHF_ENABLE_PERFORMANCE_METRICS
143#define __lhf_calc_time(__stat, __key) \
144 auto __LHF_TIMER_OBJECT__ = __CalcTime((__stat), (__key))
145#define __lhf_calc_functime(__stat) \
146 auto __LHF_TIMER_OBJECT__ = __CalcTime((__stat), __func__)
147#else
148#define __lhf_calc_time(__stat, __key)
149#define __lhf_calc_functime(__stat)
150#endif
151
152}
153
154#endif
Definition lhf.hpp:28
Utility class for enabling code-based profiling.
Definition profiling.hpp:22
Count & get_counter(const String &s)
Definition profiling.hpp:75
void timer_end(const String &s)
Definition profiling.hpp:65
Map< String, Duration > timers
Definition profiling.hpp:46
Duration & get_timer(const String &s)
Definition profiling.hpp:50
void inc_counter(const String &s)
Definition profiling.hpp:83
Map< String, Count > counters
Definition profiling.hpp:45
std::chrono::steady_clock::time_point TimePoint
Definition profiling.hpp:25
void timer_start(const String &s)
Definition profiling.hpp:58
The object used to enable the duration capturing mechanism.
__CalcTime(PerformanceStatistics &stat, const std::string key)
PerformanceStatistics & stat
const std::string key