#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
// run with printf
int main(void)
{
// begine
struct timespec begin;
timespec_get(&begin, TIME_UTC);
struct _FILETIME beginCPU, creation_time, kernel_time, exit_time;
GetProcessTimes(GetCurrentProcess(), &creation_time, &exit_time, &kernel_time, &beginCPU);
for (long i = 0; i < 10000; i++) {
printf("i = %ld\n", i);
}
/*
struct timespec
{
time_t tv_sec; // Seconds - >= 0
long tv_nsec; // Nanoseconds - [0, 999999999]
};
*/
/*
typedef struct _FILETIME {
DWORD dwLowDateTime;
DWORD dwHighDateTime;
} FILETIME, *PFILETIME, *LPFILETIME;
*/
/*
GetProcessTimes(
_In_ HANDLE hProcess,
_Out_ LPFILETIME lpCreationTime,
_Out_ LPFILETIME lpExitTime,
_Out_ LPFILETIME lpKernelTime,
_Out_ LPFILETIME lpUserTime
);
*/
// end
struct timespec end;
timespec_get(&end, TIME_UTC);
struct _FILETIME endCPU;
GetProcessTimes(GetCurrentProcess(), &creation_time, &exit_time, &kernel_time, &endCPU);
double execute_time = (end.tv_sec - begin.tv_sec) + (end.tv_nsec - begin.tv_nsec) / 1000000000.0;
ULARGE_INTEGER ulBeginCPU;
ulBeginCPU.LowPart = beginCPU.dwLowDateTime;
ulBeginCPU.HighPart = beginCPU.dwHighDateTime;
ULARGE_INTEGER ulEndCPU;
ulEndCPU.LowPart = endCPU.dwLowDateTime;
ulEndCPU.HighPart = endCPU.dwHighDateTime;
// ULONGLONG QuadPart; // typedef unsigned __int64 ULONGLONG
double execute_timeCPU = (ulEndCPU.QuadPart - ulBeginCPU.QuadPart) / 10000000.0;
printf("Execute Time (CLOCK TIME): %lf sec\n", execute_time);
printf("Execute Time (CPU TIME): %lf sec\n", execute_timeCPU);
}
/*
run:
...
i = 9992
i = 9993
i = 9994
i = 9995
i = 9996
i = 9997
i = 9998
i = 9999
Execute Time (CLOCK TIME): 4.947347 sec
Execute Time (CPU TIME): 0.031250 sec
*/