Linux Get Process Execution Time method sample _linux

Source: Internet
Author: User

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:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.