diff --git a/TinySTL/Profiler/Profiler.cpp b/TinySTL/Profiler/Profiler.cpp index 4f9a70a..a2d0fb3 100644 --- a/TinySTL/Profiler/Profiler.cpp +++ b/TinySTL/Profiler/Profiler.cpp @@ -10,18 +10,46 @@ namespace TinySTL{ void ProfilerInstance::start(){ startTime = SteadyClock::now(); } + void ProfilerInstance::finish(){ finishTime = SteadyClock::now(); duringTime = std::chrono::duration_cast(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 + } } } \ No newline at end of file diff --git a/TinySTL/Profiler/Profiler.h b/TinySTL/Profiler/Profiler.h index f46c814..952c0bd 100644 --- a/TinySTL/Profiler/Profiler.h +++ b/TinySTL/Profiler/Profiler.h @@ -3,11 +3,19 @@ #include #include +#include #include #include #include #include +#ifdef WIN32 +#include +#include +#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> DurationTime;//µ¥Î»Ãë + 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_); }; } }