-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.cpp
59 lines (46 loc) · 1.71 KB
/
test.cpp
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
#include <Windows.h>
#include <iostream>
#include <psapi.h>
#include <chrono>
// Ensure that the PSAPI library is linked.
#pragma comment(lib, "psapi.lib")
class Profiler {
private:
HANDLE m_process;
SIZE_T m_memoryUsage;
// Define a private member variable to store the start time.
const std::chrono::time_point<std::chrono::high_resolution_clock> start_;
public:
Profiler(): start_(std::chrono::high_resolution_clock::now()) {
// Get the current process.
m_process = GetCurrentProcess();
// Get the initial amount of memory used by the process.
UpdateMemoryUsage();
}
~Profiler() {
const auto end = std::chrono::high_resolution_clock::now();
const auto elapsed_time = std::chrono::duration_cast<std::chrono::milliseconds>(end - start_);
std::cout << "Elapsed time: " << elapsed_time.count() << "ms" << std::endl;
// Print the memory usage when the object goes out of scope.
PrintMemoryUsage();
}
// Update the amount of memory used by the process.
void UpdateMemoryUsage() {
PROCESS_MEMORY_COUNTERS_EX pmc;
if (GetProcessMemoryInfo(m_process, reinterpret_cast<PPROCESS_MEMORY_COUNTERS>(&pmc), sizeof(pmc))) {
m_memoryUsage = pmc.PrivateUsage;
}
}
// Print the amount of memory used by the process.
void PrintMemoryUsage() {
std::cout << "Memory usage: " << m_memoryUsage << " bytes" << std::endl;
}
};
int main() {
// Create a profiler object.
Profiler profiler;
// Do some work here. The profiler will automatically measure the memory usage.
// Update the memory usage manually if desired.
profiler.UpdateMemoryUsage();
return 0;
}