添加在windows平台下测试程序内存使用量的接口

This commit is contained in:
邹晓航
2014-10-23 13:07:13 +08:00
parent 032674ed18
commit 78a59a83b1
2 changed files with 43 additions and 0 deletions

View File

@@ -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
}
}
}

View File

@@ -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_);
};
}
}