First, Introduction
Time processing is often encountered in programming, including the running time and display time of the program. In standard C, date and time processing is included in the Time.h header file, which requires the use of date and time-dependent types of functions, which need to be imported into time.h.
Second, the example
1. Calculate Time Difference
#include <stdio.h>
#include <sys/time.h>
#include <unistd.h>
int main ()
{
struct timeval start, end;
unsigned long spend_time;
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);
Microsecond time Difference
spend_time=1000000* (end.tv_sec-start.tv_sec) + (end.tv_usec-start.tv_usec);
printf ("%ld\n", spend_time);
return 0;
}
Compile
Gcc-g-O Time_diff time_diff.c
Run
The above mentioned is the entire content of this article, I hope you can enjoy.