Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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

the code you posted is wrong. to get the expected result the then variable should be declared globally, but I would recommend to redesign the cputimer(int) function. – trylimits Oct 15, 2013 at 10:45 Often you'll want to measure the CPU-seconds of your process PLUS the kernel. Actual wall time is also usually of interest, so I usually check both. If CPU seconds is low and wall time is high, there may be some problem to look into. – Swiss Frank May 12, 2019 at 20:28

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.

@skyde That is true, I did not realize that the OP might look for the time the program was executing in user space (with more threads this might even return more time than what had actually passed on the 'wall clock'). In this case he could use his own function to get the timestamp and use the cputimer function to provide the logic (behaviour) he was looking for. – Sigroad Nov 12, 2013 at 16:54

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.