Measure CPU time on Windows using GetProcessTimes

偶尔善良 提交于 2019-12-06 11:45:54

I suggest you go with a simpler solution on Windows like this if your process does not run for too long:

#define WIN32_LEAN_AND_MEAN
#include <windows.h>

double getTime() {
  LARGE_INTEGER freq, val;
  QueryPerformanceFrequency(&freq);
  QueryPerformanceCounter(&val);
  return (double)val.QuadPart / (double)freq.QuadPart;
}

Then you could just use it like this:

double d0 = getTime();
// function to measure
double timeInMilliseconds = 1000* (getTime() - d0);

You could wrap this into a function to achieve similar behavior as your code.

double cputimer(int reset)
{
  static double startTime = 0;
  if(reset)
  {
    startTime = getTime();
    return 0.0;
  } else
  {
    return 1000* (getTime() - startTime);
  }
}

UPDATE: If the real intention was to query for the usertime one should replace the getTime() function (with the one used by the OP) but the logic in cputimer() remains the same.

The failure return value for both functions is 0, not -1.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!