<span style= "FONT-SIZE:18PX; BACKGROUND-COLOR:RGB (240, 240, 240); >
#include <sys/time.h></span>
int gettimeofday (struct TIMEVAL*TV, struct timezone *tz);
The parameter TV is the structure that holds the result of the time, and the parameter tz is used to save the time zone result:
struct timezone{
int tz_minuteswest;/* GMT to the West, Jet lag * *
int Tz_dsttime /*DST Time Correction mode */
}
timezone parameter if not used then pass in null.
The structure body timeval is defined as:
struct timeval{
long int tv_sec;//sec number
long int tv_usec;//microsecond number
}
The time it obtains is accurate to microsecond (1e-6 s) magnitude. Use Gettimeofday before and after a piece of code to calculate the code execution time:
struct timeval tv_begin, tv_end;
Gettimeofday (&tv_begin, NULL);
Foo ();
Gettimeofday (&tv_end, NULL);</span>
Time format for output refinement to microseconds
</pre><pre name= "code" class= "CPP" >
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
#include <time.h> /
*
Take the current time, accurate to microseconds;
*
/int main ()
{
struct timeval TV;
struct TM * time_ptr;
memset (&TV, 0, sizeof (timeval));
Gettimeofday (&TV, NULL);
Time_ptr = LocalTime (&tv.tv_sec);
printf ("%d-%02d-%02d%02d:%02d:%02d.%.04d\n",
time_ptr->tm_year + 1900,
Time_ptr->tm_mon + 1,
Time_ptr->tm_mday,
Time_ptr->tm_hour,
time_ptr->tm_min,
time_ptr->tm_sec,
TV.TV_USEC);
return 1;
} </span>