Statistics code execution time under Linux

Source: Internet
Author: User
Tags local time

Reprinted from: http://velep.com/archives/973.html

The run time of a statistical function or a piece of code is often encountered in software development. The running time can be used to analyze the operation efficiency and performance of the function or program segment to optimize the code accordingly.

In a UNIX environment, you often use the Gprof tool in the Binutils (GNU Binary toolset) to see the function run time. But the focus of this article is to write your own code to implement the function or program segment run time statistics. Detailed descriptions are described below.

Implementation principle

The implementation principle is simple, and the start time is recorded before the function or program segment starts running. When the run is complete, record the end time. Subtract the end time from the start time and get the total elapsed time.

Typically, a function or program segment runs for a short time, typically a US or MS level. To get a more accurate run time, it is common practice to repeat n run functions or program segments to get the total time t of N run. Single Run time = t/n. The greater the N, the more accurate the resulting run time.

The question now is how to get the start and end times, and the accuracy of the time to reach the US level.

Gettimeofday () function

The Gettimeofday () function is used to obtain the current time, and the time precision can reach the US level.

Header files: #include <sys/time.h>

function prototypes: int gettimeofday (struct timeval * TV, struct timezone * tz);

Function Description: The Gettimeofday () function returns the current time from the structure that the TV refers to, and the local time zone information is placed in the structure that TZ refers to.

Timeval Structure Definition:
struct Timeval {
time_t tv_sec;
suseconds_t tv_usec;
};

TimeZone structure Definition:
struct TimeZone {
int tz_minuteswest; */* and GREENWICH How MANY minutes difference */
int tz_dsttime; /* Daylight Saving Time status */
};

Both of these structures are defined in the/usr/include/sys/time.h file. The status represented by Tz_dsttime is as follows:

Dst_none: Do not use
Dst_usa: USA
Dst_aust: Australia
Dst_wet: Western Europe
Dst_met: Central Europe
Dst_eet: Eastern Europe
Dst_can: Canada
DST_GB: Great Britain
Dst_rum: Romania
Dst_tur: Turkey
Dst_austalt: Australia (after 1986)

function return value: Successful return 0, failure return-1, error code stored in errno.

Sample Program

The following code shows how to use the Gettimeofday () function to count the elapsed time of a function or a piece of code.

/** Test function run time.*/#include<stdio.h>#include<sys/time.h>voidFuncvoid){    Doublec =2.5641321, d =0.00158; Doubleval =0.0;  for(inti =0; I <1000000; i++) {Val+ = (c * c) + (d * d) + (2Cd); }     return;} intMainintargcChar**argv) {    structtimeval Tpstart, tpend; floatTimeuse; Gettimeofday (&Tpstart, NULL);     Func (); Gettimeofday (&tpend, NULL); Timeuse=1000000* (TPEND.TV_SEC-TPSTART.TV_SEC) + tpend.tv_usec-tpstart.tv_usec; Timeuse/=1000000; printf ("used time:%f seconds\n", Timeuse); return  0;}


This method is not only suitable for Linux programs, but also for ecos applications.

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.