添加在windows平台下测试程序内存使用量的接口
This commit is contained in:
@@ -10,18 +10,46 @@ namespace TinySTL{
|
||||
void ProfilerInstance::start(){
|
||||
startTime = SteadyClock::now();
|
||||
}
|
||||
|
||||
void ProfilerInstance::finish(){
|
||||
finishTime = SteadyClock::now();
|
||||
duringTime = std::chrono::duration_cast<DurationTime>(finishTime - startTime);
|
||||
}
|
||||
|
||||
void ProfilerInstance::dumpDuringTime(std::ostream& os){
|
||||
os << "total " << duringTime.count() * 1000 << " milliseconds" << std::endl;
|
||||
}
|
||||
|
||||
double ProfilerInstance::second(){
|
||||
return duringTime.count();
|
||||
}
|
||||
|
||||
double ProfilerInstance::millisecond(){
|
||||
return duringTime.count() * 1000;
|
||||
}
|
||||
|
||||
size_t ProfilerInstance::memory(MemoryUnit mu){
|
||||
#ifdef WIN32
|
||||
PROCESS_MEMORY_COUNTERS pmc;
|
||||
HANDLE hProcess = GetCurrentProcess();
|
||||
if (!GetProcessMemoryInfo(hProcess, &pmc, sizeof(pmc)))
|
||||
throw std::runtime_error("GetProcessMemoryInfo failed");
|
||||
size_t memory = 0;
|
||||
switch (mu){
|
||||
case MemoryUnit::KB_:
|
||||
memory = pmc.WorkingSetSize KB;
|
||||
break;
|
||||
case MemoryUnit::MB_:
|
||||
memory = pmc.WorkingSetSize MB;
|
||||
break;
|
||||
case MemoryUnit::GB_:
|
||||
memory = pmc.WorkingSetSize GB;
|
||||
break;
|
||||
}
|
||||
CloseHandle(hProcess);
|
||||
return memory;
|
||||
#else
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,11 +3,19 @@
|
||||
|
||||
#include <chrono>
|
||||
#include <ctime>
|
||||
#include <exception>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <ratio>
|
||||
#include <utility>
|
||||
|
||||
#ifdef WIN32
|
||||
#include <Windows.h>
|
||||
#include <Psapi.h>
|
||||
#pragma comment(lib, "psapi.lib")
|
||||
#else
|
||||
#endif
|
||||
|
||||
namespace TinySTL{
|
||||
namespace Profiler{
|
||||
|
||||
@@ -16,6 +24,11 @@ namespace TinySTL{
|
||||
typedef std::chrono::steady_clock SteadyClock;
|
||||
typedef SteadyClock::time_point TimePoint;
|
||||
typedef std::chrono::duration<double, std::ratio<1, 1>> DurationTime;//<2F><>λ<EFBFBD><CEBB>
|
||||
enum class MemoryUnit{KB_, MB_, GB_};
|
||||
private:
|
||||
#define KB / 1024
|
||||
#define MB KB / 1024
|
||||
#define GB MB / 1024
|
||||
private:
|
||||
static DurationTime duringTime;
|
||||
static TimePoint startTime;
|
||||
@@ -27,6 +40,8 @@ namespace TinySTL{
|
||||
|
||||
static double second();
|
||||
static double millisecond();
|
||||
|
||||
static size_t memory(MemoryUnit mu = MemoryUnit::KB_);
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user