I'm trying to measure the CPU and Wall time for my program.
The code should run on Windows so it's alright to use platform specific functions.
For Wall time I use QueryPerformanceCounter()
and it is precise.
When I use GetProcessTimes()
I get a 15.625 millisecond precision.
On MSDN it says that the precision of the returned CPU time is 100 nanoseconds.
Here is the code I am using:
void getCPUtime(unsigned long long *pUser, unsigned long long *pKernel) {
FILETIME user, kernel, exit, start;
ULARGE_INTEGER userCPU, kernelCPU;
if (::GetProcessTimes(::GetCurrentProcess(), &start, &exit, &kernel, &user) != 0) {
userCPU.LowPart = user.dwLowDateTime;
userCPU.HighPart = user.dwHighDateTime;
kernelCPU.LowPart = kernel.dwLowDateTime;
kernelCPU.HighPart = kernel.dwHighDateTime;
}
*pUser = (unsigned long long)userCPU.QuadPart;
*pKernel = (unsigned long long)kernelCPU.QuadPart;
}
And I am calling it from:
void someFunction() {
unsigned long long *userStartCPU, *userEndCPU, *kernelStartCPU, *kernelEndCPU;
double userCPUTime, kernelCPUTime;
getCPUtime(userStartCPU, kernelStartCPU);
// Do stuff which takes longer than a millisecond
getCPUtime(userEndCPU, kernelEndCPU);
userCPUTime = (userEndCPU - userStartCPU) / (double)10000.00; // Convert to milliseconds
kernelCPUTime = (kernelEndCPU - kernelStartCPU) / (double)10000.00; // Convert to milliseconds
}
Does anyone know why this is happening, or has any other way to precisely measure CPU time on Windows?