In linux, the C language obtains the high-precision gettimeofday function, and the high-precision gettimeofday function.
Preface:
Gettimeofday is used to obtain the current clock because you need to know each function or some devices perform commands during development.
1. Function Description
# Include
Int gettimeofday (struct timeval * TV, struct timezone * tz );
Note:
1. accuracy level,
SubtleLevel
2. Affected by system time modification
3. The number of seconds returned is from 00:00:00, January 1, January 1, 1970.
The TV parameter is the structure used to save the time result. The tz parameter is used to save the time zone result:
The struct timeval is defined:
Click (here) to fold or open
Struct timeval {long int TV _sec; // number of seconds long int TV _usec; // Number of microseconds}
The obtained time is accurate
Microsecond (1e-6 s) Magnitude
The struct timezone is defined:
Click (here) to fold or open
Struct timezone {int tz_minuteswest;/* Greenwich Mean Time difference to the west */int tz_dsttime;/* DST time correction method */}
If the timezone parameter is not used
NULLYou can.
The value of tz_dsttime is as follows:
Click (here) to fold or open
DST_NONE/* not used */DST_USA/* us */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) */
Return Value
If the call succeeds, 0 is returned. If the call fails,-1 is returned.The error code is stored in errno.
ERRORS
EFAULT One of TV or tz pointed outside the accessible address space.
EINVAL Timezone (or something else) is invalid.
Ii. Instances
Click (here) to fold or open
1 # include <stdio. h> 2 # include <stdlib. h> 3 # include <string. h> 4 # include <sys/time. h> 5 int time_substract (struct timeval * result, struct timeval * begin, struct timeval * end) 6 {7 if (begin-> TV _sec> end-> TV _sec) return-1; 8 if (begin-> TV _sec = end-> TV _sec) & (begin-> TV _usec> end-> TV _usec) return-2; 9 result-> TV _sec = (end-> TV _sec-begin-> TV _sec); 10 result-> TV _usec = (end-> TV _usec-begin-> TV _usec ); 11 12 if (result-> TV _usec <0) 13 {14 result-> TV _sec --; 15 result-> TV _usec + = 1000000; 16} 17 return 0; 18} 19 int main (int argc, char ** argv) 20 {21 struct timeval start, stop, diff; 22 memset (& start, 0, sizeof (struct timeval )); 23 memset (& stop, 0, sizeof (struct timeval); 24 memset (& diff, 0, sizeof (struct timeval); 25 gettimeofday (& start, 0 ); 26 // do what you want to do... 27 printf ("hello world \ n"); 28 gettimeofday (& stop, 0); 29 time_substract (& diff, & start, & stop); 30 printf ("Total time: % d s, % d us \ n ", (int) diff. TV _sec, (int) diff. TV _usec); 31}
Operation Result:
Reprinted from: http://blog.chinaunix.net/uid-28458801-id-4214306.html