Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Computing CPU time in C++ on Windows

Is there any way in C++ to calculate how long does it take to run a given program or routine in CPU time?

I work with Visual Studio 2008 running on Windows 7.

like image 558
Vicent Avatar asked Sep 26 '12 15:09

Vicent


2 Answers

If you want to know the total amount of CPU time used by a process, neither clock nor rdtsc (either directly or via a compiler intrinsic) is really the best choice, at least IMO. If you need the code to be portable, about the best you can do is use clock, test with the system as quiescent as possible, and hope for the best (but if you do, be aware that the resolution of clock is CLOCKS_PER_SEC, which may or may not be 1000, and even if it is, your actual timing resolution often won't be that good -- it may give you times in milliseconds, but at least normally advance tens of milliseconds at a time).

Since, however, you don't seem to mind the code being specific to Windows, you can do quite a bit better. At least if my understanding of what you're looking for is correctly, what you really want is probably GetProcessTimes, which will (separately) tell you both kernel-mode and user-mode CPU usage of the process (as well as the start time and exit time, from which you can compute wall time used, if you care). There's also QueryProcessCycleTime, which will tell you the total number of CPU clock cycles used by the process (total of both user and kernel mode in all threads). Personally, I have a hard time imagining much use for the latter though -- counting individual clock cycles can be useful for small sections of code subject to intensive optimization, but I'm less certain about how you'd apply it to a complete process. GetProcessTimes uses FILETIME structures, which support resolutions of 100 nanoseconds, but in reality most times you'll see will be multiples of the scheduler's time slice (which varies with the version of windows, but is on the order of milliseconds to tens of milliseconds).

In any case, if you truly want time from beginning to end, GetProcessTimes will let you do that -- if you spawn the program (e.g., with CreateProcess), you'll get a handle to the process which will be signaled when the child process exits. You can then call GetProcessTimes on that handle, and retrieve the times even though the child has already exited -- the handle will remain valid as long as at least one handle to the process remains open.

like image 184
Jerry Coffin Avatar answered Sep 20 '22 23:09

Jerry Coffin


Here's one way. It measures routine exeution time in milliseconds.

clock_t begin=clock(); starts before the route is executed and clock_t end=clock(); starts right after the routine exits.

The two time sets are then subtracted from each other and the result is a millisecod value.

#include <stdio.h>
#include <iostream>
#include <time.h>
using namespace std;

double get_CPU_time_usage(clock_t clock1,clock_t clock2)
{
    double diffticks=clock1-clock2;
    double diffms=(diffticks*1000)/CLOCKS_PER_SEC;
    return diffms;
} 

void test_CPU_usage()
{
  cout << "Standby.. measuring exeution time:  ";

  for (int i=0; i<10000;i++)
  {
        cout << "\b\\" << std::flush;
        cout << "\b|" << std::flush;
        cout << "\b/" << std::flush;
        cout << "\b-" << std::flush;
  }

  cout << " \n\n";
}

int main (void)
{

    clock_t begin=clock();

    test_CPU_usage();

    clock_t end=clock();

    cout << "Time elapsed: " << double(get_CPU_time_usage(end,begin)) << " ms ("<<double(get_CPU_time_usage(end,begin))/1000<<" sec) \n\n";
    return 0;
}
like image 20
Software_Designer Avatar answered Sep 18 '22 23:09

Software_Designer