0

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?

  1. the CPU time required by only process A (it's what I'd like to have...)
  2. the CPU time required by all the processes running on the same thread of process A
  3. 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?

user1403546
  • 1,379
  • 1
  • 16
  • 32
  • Take a look at http://unixhelp.ed.ac.uk/CGI/man-cgi?getrusage+2 – OldProgrammer Dec 18 '14 at 18:12
  • I you have it written... Why don't you run it and check? Also you heavily confuse terms: process, thread, calculation and measurement. I find it quite hard to understand, what do you really ask about, and what is your use-case. – luk32 Dec 18 '14 at 18:24

0 Answers0