#include "stdafx.h"

#ifdef _WIN32
#include "windows.h"
#endif
#ifndef _WIN32
#include <sys/resource.h>
#include <unistd.h>
#endif

#ifdef _MSC_VER
#pragma hdrstop
#endif

float GetMyCPUTime(void)
{
#ifdef _WIN32
	HANDLE hProcess;	// specifies the process of interest
    FILETIME lpCreationTime;	// when the process was created
    FILETIME lpExitTime;	// when the process exited
    FILETIME lpKernelTime;	// time the process has spent in kernel mode
    FILETIME lpUserTime;
    LARGE_INTEGER t1, t2;

    hProcess = GetCurrentProcess();
    GetProcessTimes(hProcess, &lpCreationTime, &lpExitTime, &lpKernelTime, &lpUserTime);
    memcpy(&t1, &lpUserTime, sizeof(FILETIME));
    memcpy(&t2, &lpKernelTime, sizeof(FILETIME));
    return ((float)(t1.QuadPart+t2.QuadPart)/10000000);
#endif
#ifndef _WIN32
	float res;
	struct rusage usage;
	getrusage(RUSAGE_SELF, &usage);
	res = usage.ru_utime.tv_usec + usage.ru_stime.tv_usec;
	res *= 1e-6;
	res += usage.ru_utime.tv_sec + usage.ru_stime.tv_sec;
	return res;
#endif
}

int GetNoOfCPUs(void) {
#ifdef _WIN32
	SYSTEM_INFO sysinfo;
	GetSystemInfo(&sysinfo);
	return sysinfo.dwNumberOfProcessors;
#endif
#ifndef _WIN32
	return sysconf( _SC_NPROCESSORS_ONLN );
#endif
}
