Added time and memory measurement for windows. (#4)

This commit was merged in pull request #4.
This commit is contained in:
Marcin Piątkowski
2025-05-28 16:21:56 +02:00
committed by GitHub
parent fafe8dbe07
commit 90c8e4f4c0

View File

@@ -5,7 +5,12 @@
#define __MEMTIME_H__
#include <sys/time.h>
#ifdef __WIN32__
#include <windows.h>
#include <psapi.h>
#else
#include <sys/resource.h>
#endif
#if defined(__APPLE__)
#include <malloc/malloc.h>
#endif
@@ -17,6 +22,25 @@ using namespace std;
typedef long long int64;
#ifdef __WIN32__
static inline double cpuTime()
{
FILETIME createTime, exitTime, kernelTime, userTime;
if (!GetProcessTimes(GetCurrentProcess(), &createTime, &exitTime, &kernelTime, &userTime)) {
return 0.0;
}
ULARGE_INTEGER u;
u.LowPart = userTime.dwLowDateTime;
u.HighPart = userTime.dwHighDateTime;
return (double)u.QuadPart / 10e6;
}
#else
static inline double cpuTime(void)
{
struct rusage ru;
@@ -24,6 +48,8 @@ static inline double cpuTime(void)
return (double)ru.ru_utime.tv_sec + (double)ru.ru_utime.tv_usec / 1000000;
}
#endif
static inline int memReadStat(void)
{
char name[256];
@@ -47,11 +73,28 @@ static inline int memReadStat(void)
return value;
}
#ifdef __WIN32__
static inline int64 memUsedInt64()
{
PROCESS_MEMORY_COUNTERS pmc;
if (GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(pmc))) {
return (int64) pmc.WorkingSetSize;
}
return 0;
}
#else
static inline int64 memUsedInt64()
{
return (int64)memReadStat() * (int64)getpagesize();
}
#endif
#if defined(__APPLE__)
static inline double memUsed()