计时器
#include <ctime>
struct _stopwatch {
size_t t = clock();
void reset() { t = clock(); }
operator size_t() {
return clock() - t;
}
};
内存监控
#define NOMINMAX
#include <iostream>
#include <windows.h>
#include <psapi.h>
#include <thread>
#include <limits>
inline SIZE_T getMemory() {
HANDLE process = GetCurrentProcess();
PROCESS_MEMORY_COUNTERS_EX pmc;
if (GetProcessMemoryInfo(process, reinterpret_cast<PROCESS_MEMORY_COUNTERS*>(&pmc), sizeof(pmc))) {
SIZE_T virtualMemUsedByMe = pmc.PrivateUsage;
return virtualMemUsedByMe;
}
else {
std::cout << "Failed to get process memory info." << std::endl;
}
return 0;
}
class memory_monitor {
public:
memory_monitor(): stop_(true), min_(0), max_(0){}
~memory_monitor() {
stop_ = true;
t.join();
}
void start(int period = 200) {
stop_ = false;
min_ = std::numeric_limits<SIZE_T>::max();
max_ = std::numeric_limits<SIZE_T>::min();
t = std::thread([&]() {
while (!stop_) {
auto mem_size = getMemory();
min_ = (std::min)(min_, mem_size);
max_ = (std::max)(max_, mem_size);
std::this_thread::sleep_for(std::chrono::milliseconds(period));
}
});
}
SIZE_T stop() {
stop_ = true;
t.join();
return max_ - min_;
}
private:
bool stop_;
std::thread t;
SIZE_T min_, max_;
};