Get microsecond time in Linux with C language
1. Data structure
int gettimeofday (structstruct timezone *tz);
The parameter TV is the structure that holds the result of the acquisition time, and the parameter tz is used to save the time zone result:
struct timezone{int tz_minuteswest; /* Time difference between GMT and West */ int tz_dsttime; /* How to fix DST time */ }
The timezone parameter is passed in NULL if it is not used.
The structure timeval is defined as:
struct timeval{longint/ s number of longint/ μs }
2. code example Temp.cpp
#include <stdio.h>//For printf ()#include <sys/time.h>//For Gettimeofday ()#include <unistd.h>//For sleep ()intMain () {structtimeval start, end; Gettimeofday (&start, NULL); printf ("Start:%d.%d\n", Start.tv_sec, start.tv_usec); Sleep (1); Gettimeofday (&end, NULL); printf ("End:%d.%d\n", End.tv_sec, end.tv_usec); return 0;}
3. Compiling
g++-O temp temp.cpp
4. Operation and Results
$./1418118324.633128end 1418118325.634616
Storm
Mail: [Email protected]
C-language acquisition of microsecond time in Linux