-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
101 lines (84 loc) · 2.31 KB
/
main.cpp
File metadata and controls
101 lines (84 loc) · 2.31 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
#include <iostream>
#include <unordered_map>
#include <vector>
#include <thread>
#include <chrono>
#include <string>
struct FunctionProfilerData;
std::unordered_map<std::thread::id, std::vector<FunctionProfilerData>> profiler;
struct FunctionProfilerData
{
std::string name;
uint32_t frame;
std::chrono::time_point<std::chrono::high_resolution_clock> start;
std::chrono::time_point<std::chrono::high_resolution_clock> end;
bool operator<(const FunctionProfilerData& other) const
{
return start < other.start;
}
bool operator==(const FunctionProfilerData& other) const
{
return start == other.start;
}
bool operator<=(const FunctionProfilerData& other) const
{
return start <= other.start;
}
};
struct FunctionProfiler
{
FunctionProfilerData functionData;
FunctionProfiler(const char* _name, uint32_t _frame) :
functionData(_name, _frame, std::chrono::high_resolution_clock::now())
{
std::cout << "Created!" << std::endl;
}
~FunctionProfiler()
{
functionData.end = std::chrono::high_resolution_clock::now();
profiler[std::this_thread::get_id()].push_back( std::move(functionData) );
}
};
#ifndef NDEBUG
#define PROFILE_FUNCTION(frame) FunctionProfiler prof_obj(__func__, frame);
#else
#define PROFILE_FUNCTION(frame)
#endif
void lightFunction()
{
PROFILE_FUNCTION(0)
for (int i=0; i<49999; i++)
{
volatile int x = i * 2;
}
}
void heavyFunction()
{
PROFILE_FUNCTION(0)
for (int i=0; i<99999; i++)
{
volatile int y = i / 2;
}
}
int main()
{
{
PROFILE_FUNCTION(0)
lightFunction();
heavyFunction();
std::cout << "Hello Wolrd!" << std::endl;
}
for (const auto& thread_data : profiler)
{
std::cout << "Thread " << thread_data.first << ": " << std::endl;
auto& functionsProfilersData = thread_data.second;
for(const auto& functionData : functionsProfilersData)
{
std::cout << "\t" << functionData.name << std::endl;
std::cout << "\t\t Start: " << functionData.start << std::endl;
std::cout << "\t\t End: " << functionData.end << std::endl;
std::cout << "\t\t Frame: " << functionData.frame << std::endl;
}
}
return 0;
}