Get Process Execution Time
First, the concept of 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.
Ii. Methods of Access
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; /* user time */ clock_t Tms_stime; /* system time */ clock_t Tms_cutime; /* user time of children */ clock_t Tms_cstime; /* system time of children */ };
#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.
Third, test examples
The test procedure is as follows:
#include <stdio.h>#include<stdlib.h>#include<sys/times.h>#include<unistd.h>#defineBuffer_size 4 * 1024intMain () {intSc_clk_tck; Sc_clk_tck=sysconf (_SC_CLK_TCK); structTMS 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:
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.
Reference: http://www.cnblogs.com/Anker/p/3416288.html
Linux-Get Process Execution time