Article Title: time-based operation functions accurate to microseconds in Linux. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
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 */ }; |