2

Possible Duplicate:
What specifically are wall-clock-time, user-cpu-time, and system-cpu-time in UNIX?

The following program will run a command and then display various values about how much time it used:

#include <iostream>
#include <sys/times.h>
using namespace std;

static void pr_times(clock_t, struct tms *, struct tms *);
static void do_cmd(char *);

int main(int argc, char *argv[])
{
    int i;
    setbuf(stdout, NULL);
    for (i = 1; i < argc; i++)
        do_cmd(argv[i]);    /* once for each command-line arg */
    exit(0);
}

    static void
do_cmd(char *cmd)        /* execute and time the "cmd" */
{
    struct tms  tmsstart, tmsend;
    clock_t     start, end;
    int         status;

    printf("\ncommand: %s\n", cmd);

    if ((start = times(&tmsstart)) == -1)    /* starting values */
        cout << "times error" << endl; 

    if ((status = system(cmd)) < 0)     /* execute command */
        cout << "system error" << endl; 

    if ((end = times(&tmsend)) == -1)
        cout << "times error" << endl;                                          
    pr_times(end-start, &tmsstart,
            &tmsend);
    exit(status);
}
    static void
pr_times(clock_t real, struct tms *tmsstart, struct tms *tmsend)
{
    static long     clktck = 0;

    if (clktck == 0)    /* fetch clock ticks per second first time */
        if ((clktck = sysconf(_SC_CLK_TCK)) < 0)
            cout << "sysconf error" << endl;
    printf(" real:  %7.2f\n", real / (double) clktck);
    printf(" user:  %7.2f\n",
            (tmsend->tms_utime - tmsstart->tms_utime) / (double) clktck);
    printf(" sys:   %7.2f\n",
            (tmsend->tms_stime - tmsstart->tms_stime) / (double)
            clktck);
    printf(" child user:   %7.2f\n",
            (tmsend->tms_cutime - tmsstart->tms_cutime) /
            (double) clktck);
    printf(" child sys:    %7.2f\n",
            (tmsend->tms_cstime - tmsstart->tms_cstime) /
            (double) clktck);
}

Here's an example of the output:

 real:     1.44
 user:     0.00
 sys:      0.00
 child user:      0.03
 child sys:       0.00

The real time is how much time total has elapsed, correct? What are the other values?

Community
  • 1
  • 1
node ninja
  • 31,796
  • 59
  • 166
  • 254

1 Answers1

20

Real time is the time as measured by a wall clock, that is, the time between the instant that the process was spawned until it ended. User time is how much CPU time was spent on the process in user mode, and sys is the CPU time that was spent in the OS while performing actions requested by the process.

To provide some different examples of what that means, when a process calls sleep(10), the real time spent in the process grows by 10 seconds, but no actual work was done by the process itself, so neither user nor sys time are incremented. Calls to the operating system, as reading or writing to disk, acquiring or releasing memory or any other OS operation will imply that the CPU is busy working in the system for the process, so that will add to sys time (and real time), but will not add to the user time. A complex mathematical calculation performed inside the process will take user time (process time) but will not spent any CPU cycles in the OS, so it will add to user time (and real time), but not to sys time.

David Rodríguez - dribeas
  • 204,818
  • 23
  • 294
  • 489