I'm running tests for a C++ algorithm on a Ubuntu 6-core machine. I would like, for each test, measure the exact CPU time required for ONLY that test process, and not fron other tests or computer processes running on the same machine. For this purpose I'm currently using this class:
#include <time.h>
#include <sys/times.h>
#include <iostream>
struct stopwatch
{
clock_t tstart;
clock_t tprevlap;
clock_t tlap;
tms ustart;
tms uprevlap;
tms ulap;
unsigned int clk_tk;
stopwatch() :
tstart(clock()), // clock()
tprevlap(tstart),
tlap(tstart),
ustart(),
uprevlap(),
ulap(),
clk_tk(sysconf(_SC_CLK_TCK))
{
times(&ustart);
std::copy(&ustart, &ustart + 1, &uprevlap);
std::copy(&ustart, &ustart + 1, &ulap);
};
void lap()
{
std::copy(&ulap, &ulap + 1, &uprevlap);
tprevlap = tlap;
times(&ulap);
tlap = clock();
}
double partial_user_time()
{
return ((double)ulap.tms_utime-ustart.tms_utime)/clk_tk;
}
double lap_user_time()
{
return ((double)(ulap.tms_utime-uprevlap.tms_utime))/clk_tk;
}
};
Given a process A, what does clock() function exactly measure?
- the CPU time required by only process A (it's what I'd like to have...)
- the CPU time required by all the processes running on the same thread of process A
- the CPU time required by all the processes on all the threads of the machine
E.g. if stopwatch calculates 100 seconds for test A when running test A is the only test running on the machine, if I launch test A plus test B, C and D, would the time calculated for test A be again 100 or not?