Time operation functions accurate to microseconds in Linux

Source: Internet
Author: User
Linux time-based operation functions accurate to microseconds-general Linux technology-Linux programming and kernel information. The following is a detailed description. In Linux, if the time is computed in seconds, I believe everyone has used time and other functions to implement it. But what should I do more accurately? What about milliseconds and microseconds?

Take a look at the following source code to understand:

# Include
# Include
# Include

Void function ()/* is a function that consumes a certain amount of time and has no practical use */
{
Unsigned int I, j;
Double y;
For (I = 0; I <10000; I ++)
For (j = 0; j <10000; j ++)
Y = sin (double) I );
}

Int main (int argc, char ** argv)
{
Struct timeval tpstart, tpend;
Float timeuse;

Gettimeofday (& tpstart, NULL );
Function ();
Gettimeofday (& tpend, NULL );
Timeuse = 1000000 * (tpend. TV _sec-tpstart. TV _sec) + tpend. TV _usec-tpstart. TV _usec;
Timeuse/= 1000000;
Printf ("Used Time: % f \ n", timeuse );
Exit (0 );
}


The gettimeofday function is used, and this structure is used in the function:

Struct timeval {
Long TV _sec;/* seconds */
Long TV _usec;/* microseconds */
};
Needless to say, Do you understand everything else?
Related Article

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.