1. Preface
Test the execution time of a program, including user CPU time, system CPU time, and clock time. Before the time is obtained before the main function of the program is implemented with the time function, this can only roughly calculate the program execution times, can not accurately get other time. When watching "Apue", the book about the program time test procedures, very formal, provide these three time. If so, search the internet for a moment and summarize it.
2. How to obtain
There are two ways to get it, the first is to use the time command, the time process. The second is by logging in the program, first using the sysconf function to get the clock tick number, and then using the Times to get the TMS structure.
To see the Times function, Man 2 TMS, get the TMS structure definition and the Times function declaration as follows:
struct TMS { clock_t tms_utime; /**//**//**//**/ };
#include <sys/times.h> clock_t times (struct TMS *buf);
Note: The time calculated here is the number of clock ticks, which need to be divided by the number of ticks in the system clock to derive the actual number of seconds.
3. Test Example:
The test procedure is as follows:
1 #include <stdio.h>2 #include <stdlib.h>3 #include <sys/times.h>4 #include <unistd.h>56#define BUFFER_SIZE 4 * 102478IntMain ()9{10IntSC_CLK_TCK;One SC_CLK_TCK =Sysconf (_SC_CLK_TCK);1213structTMS Begin_tms, End_tms;14clock_t begin, end;System ("Date");begin = Times (&BEGIN_TMS);Sleep (2);End = Times (&END_TMS);19printf ("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);25 printf ( "child User Time :%lf\n "26 (end_tms.tms_ Cutime-begin_tms.tms_cutime)/(double27 printf ( "child sys time:%LF \n "28 (end_tms.tms_ Cstime-begin_tms.tms_cstime)/(double29 return 0; 30}
The test results are as follows:
With the time command, the test results are as follows:
4. Reference website
http://www.01happy.com/linux-process-time/
http://www.01happy.com/c-get-process-time/
Linux viewing process clock time, user CPU time, and system CPU time
When programming under Linux, it may involve measuring the execution time of the process. The time value of the process under Linux is divided into three types:
- Clock time: Refers to the time that the process was actually executed from the start to the end.
- User CPU time: Refers to the time spent executing user instructions in the process, and also includes child processes.
- System CPU Time: The time it takes for a process to execute a kernel program, such as when the read and write kernel methods are called, and the time spent is counted into the system CPU time.
Under Linux, you can use the time command to view the consumption of these three times values when the program executes. The author has written a test program to demonstrate this process:
#include <stdio.h>
int
main(
void
)
{
int
i;
while
(i <= 10E7) {
i++;
}
return
1;
}
The program is very simple, do not explain, compiled into a binary file a.out, using the time command to execute, on the author's computer to enter the following information:
$ time./a.outreal 0m0.349suser 0m0.340ssys 0m0.004s
Where real represents the clock time, user represents the CPU time, and SYS represents the system CPU time. The time command can also be used for system commands, such as time LS, time PS, and so on.
C language Gets clock time, user CPU time, and system CPU time
The Times function can be used to obtain these three kinds of time in C language, which is described as follows:
#include <sys/times.h>
clock_t
times(
struct
tms *buf);
The structure of the parameter TMS is 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 */
};
The time is measured in ticks (clock tick), and details can be viewed with the man 2 times to see the Help manual. The following example is used to calculate the three time values that are consumed by the Execute system command date.
#include <stdio.h>
#include <sys/times.h>
#include <unistd.h>
#include <stdlib.h>
int
main(
void
)
{
//获取滴答数,在ubuntu 12.04下为100
int
clktck = 0;
if
((clktck = sysconf(_SC_CLK_TCK)) < 0) {
printf
(
"%s\n"
,
"sysconf error"
);
exit
(0);
}
struct
tms tmsstart, tmsend;
clock_t
start, end;
//获取开始时间
if
((start = times(&tmsstart)) == -1) {
printf
(
"%s\n"
,
"times error"
);
exit
(0);
}
//执行系统函数date
system
(
"date"
);
//获取结束时间
if
((end = times(&tmsend)) == -1) {
printf
(
"%s\n"
,
"times error"
);
exit
(0);
}
printf
(
"real: %7.2f\n"
, (end - start)/(
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);
return
1;
}
Compile and execute the above program with the output as follows:
$./a.outsun Dec 9 12:50:39 CST 2012real: 0.01user: 0.00sys: 0.00child User: 0.00child sys: 0.00
Where child user is the user CPU time consumed by the date command, and child SYS is the system CPU time consumed by the date command. Both values are found here as 0 because the tick count is 100, only two bits after the decimal point, and date is executed very quickly, so it is 0. How exactly to 3 digits after the decimal point?
Linux Get Process Execution time