Timing in Linux -- gettimeofday Function

Source: Internet
Author: User

Use C language for timing
In user space, you can use the C language function gettimeofday to get the time. The call format is:

# Include <sys/time. h>
Int gettimeofday (struct timeval * TV, struct timezone * tz );
Int settimeofday (const struct timeval * TV, const struct timezone * tz );
The timeval structure is defined:
Strut timeval {long TV _sec;/* seconds */long TV _usec;/* microseconds */};

It can be seen that with this method, the precision can reach microseconds, that is, 10-6 seconds. During timing, we need to call gettimeofday twice before and after, and then calculate the difference value in the middle:

Gettimeofday (& START, null );
Foo ();
Gettimeofday (& End, null );
Timeuse = 1000000 * (end. TV _sec-start. TV _sec) + end. TV _usec-start. TV _usec;
Timeuse/= 1000000;

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.