Time operation functions accurate to microseconds in Linux

Source: Internet
Author: User

 

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 <sys/time. h> # include <stdio. h> # include <math. h> void function ()/* is used to consume a certain amount of time. It is useless. */{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", 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 */       };
       

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.