Measure CPU time on Windows using GetProcessTimes

人走茶凉 提交于 2020-01-14 02:09:26

问题


I would like to measure CPU time of some function. I know how to use GetProcessTimes, but I have a problem implementing it with some kind of 'restarting':

Normally, I would do it like this:

#include "stdafx.h"
#include <math.h>
#include <windows.h>

double cputimer()
{
    FILETIME createTime;
    FILETIME exitTime;
    FILETIME kernelTime;
    FILETIME userTime;

    if ( GetProcessTimes( GetCurrentProcess( ),
        &createTime, &exitTime, &kernelTime, &userTime ) != -1 )
    {
        SYSTEMTIME userSystemTime;
        if ( FileTimeToSystemTime( &userTime, &userSystemTime ) != -1 )
            return (double)userSystemTime.wHour * 3600.0 +
            (double)userSystemTime.wMinute * 60.0 +
            (double)userSystemTime.wSecond +
            (double)userSystemTime.wMilliseconds / 1000.0;
    }
}

int _tmain(int argc, _TCHAR* argv[])
{
    double start, stop;
    long sum = 0L;

    start = cputimer();
    for (long long i = 1; i < 10000000; i++){
        sum += log((double)i);
    }
    stop = cputimer();

    printf("Time taken: %f [seconds]\n", stop - start);

    return 0;
}

But with 'reset' I'm not sure if I have right results:

#include "stdafx.h"
#include <math.h>
#include <windows.h>

#define START    1
#define STOP    0

double cputimer(int reset)
{
    FILETIME createTime;
    FILETIME exitTime;
    FILETIME kernelTime;
    FILETIME userTime;

    double now = 0, then = 0;

    if ( GetProcessTimes( GetCurrentProcess( ),
        &createTime, &exitTime, &kernelTime, &userTime ) != -1 )
    {
        SYSTEMTIME userSystemTime;
        if ( FileTimeToSystemTime( &userTime, &userSystemTime ) != -1 )
            now = (double)userSystemTime.wHour * 3600.0 +
            (double)userSystemTime.wMinute * 60.0 +
            (double)userSystemTime.wSecond +
            (double)userSystemTime.wMilliseconds / 1000.0;
    }

    if(reset)
    {
        then = now;
    }
    else
    {
        then = now - then;
    }

    return then;
}

int _tmain(int argc, _TCHAR* argv[])
{
    double s;
    long sum = 0L;

    cputimer(START);
    for (long long i = 1; i < 10000000; i++){
        sum += log((double)i);
    }
    s = cputimer(STOP);


    printf("Time taken: %f [seconds]\n", s);

    return 0;
}

Am I doing it correctly?


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/19378805/measure-cpu-time-on-windows-using-getprocesstimes

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