测量在Windows上使用GetProcessTimes CPU时间 [英] Measure CPU time on Windows using GetProcessTimes

查看:877
本文介绍了测量在Windows上使用GetProcessTimes CPU时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想测量 CPU 一些功能的时间。我知道如何使用 GetProcessTimes ,但我有某种重新启动的实施是一个问题:

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?

推荐答案

我建议你像这样在Windows上一个简单的解决方案去,如果过程不运行的时间过长:

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);

您可以换到这一点的函数来实现类似的行为作为你的code。

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);
  }
}

更新:如果真正意图是要查询的的 usertime 应该替换的getTime()功能(由OP使用的),但在逻辑 cputimer()保持不变。

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.

这篇关于测量在Windows上使用GetProcessTimes CPU时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆