-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprofiler_simple.cpp
More file actions
35 lines (29 loc) · 1.06 KB
/
Copy pathprofiler_simple.cpp
File metadata and controls
35 lines (29 loc) · 1.06 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
#include "profiler_simple.h"
Profiler::~Profiler() {
report();
}
void Profiler::record(const std::string &functionName, double duration) {
auto &data = getData()[functionName];
data.callCount++;
data.totalTime += duration;
}
void Profiler::report() {
std::cout << "\n--- Profiling Report ---\n";
for (const auto &[func, data] : getData()) {
std::cout << func << " | Calls: " << data.callCount
<< " | Total Time: " << data.totalTime << "s"
<< " | Avg Time: " << (data.totalTime / data.callCount) << "s\n";
}
}
std::unordered_map<std::string, Profiler::ProfileData> &Profiler::getData() {
// static std::unordered_map<std::string, ProfileData> data;
return data;
}
ProfileScope::ProfileScope(const std::string &funcName) : functionName(funcName) {
start = std::chrono::high_resolution_clock::now();
}
ProfileScope::~ProfileScope() {
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> duration = end - start;
__profiler.record(functionName, duration.count());
}