1. Preface
Test the execution time of a program, including user CPU time, system CPU time, clock time. Before the acquisition time is in the program's main function with a time function, this can only be a rough calculation of the execution of the program, can not accurately obtain other time. In reading "Apue", the book on the program Time test procedures, very formal, to provide these three time. If so, search the Internet and make a summary.
2, obtain the method
There are two ways to get it, the first of which is to use the time command, the time process. The second is by recording in the program, the first use of the sysconf function to get the clock tick, and then use the Times to obtain the TMS structure.
See the Times function, Man 2 TMS, get the TMS structure definition and the Times function declaration as follows:
Copy Code code as follows:
struct TMS {
clock_t Tms_utime; /* User Time * *
clock_t Tms_stime; /* System Time * *
clock_t Tms_cutime; /* User Time of children * *
clock_t Tms_cstime; /* System Time of children * *
};
Copy Code code as follows:
#include <sys/times.h>
clock_t times (struct TMS *buf);
Note: The time calculated here is the clock tick number, which needs to be divided by the system clock tick number to get the actual number of seconds.
3, test examples:
The test procedure is as follows:
Copy Code code as follows:
#include <stdio.h>
#include <stdlib.h>
#include <sys/times.h>
#include <unistd.h>
#define BUFFER_SIZE 4 * 1024
int main ()
{
int sc_clk_tck;
SC_CLK_TCK = sysconf (_SC_CLK_TCK);
struct TMS Begin_tms, End_tms;
clock_t begin, end;
System ("date");
begin = times (&BEGIN_TMS);
Sleep (2);
end = times (&END_TMS);
printf ("Real Time:%lf\n", (end-begin)/(double) sc_clk_tck);
printf ("User time:%lf\n",
(end_tms.tms_utime-begin_tms.tms_utime)/(double) sc_clk_tck);
printf ("sys time:%lf\n",
(end_tms.tms_stime-begin_tms.tms_stime)/(double) sc_clk_tck);
printf ("Child User Time:%lf\n",
(end_tms.tms_cutime-begin_tms.tms_cutime)/(double) sc_clk_tck);
printf ("Child sys Time:%lf\n",
(end_tms.tms_cstime-begin_tms.tms_cstime)/(double) sc_clk_tck);
return 0;
}
The test results are as follows:
With the time command, the test results are as follows: