#include <stdio.h>
#include <sys.time.h>
#include <time.h>
int main(int argc, char* argv[]){
struct timeval struc_start, struc_end;
long dif_time = 0;
//get start time
gettimeofday(&struc_start, NULL);
long start = ((long)struc_start.tv_sec)*1000 + (long)struc_start.tv_usec/1000;
printf("\tstart time:%ld ms\n", start);
sleep(10);
//get end time
gettimeofday(&struc_end, NULL);
long end = ((long)struc_end.tv_sec)*1000 + (long)struc_end.tv_usec/1000;
printf("\tend time:%ld ms\n", end);
//calculate time slot
dif_time = end - start;
printf("\tdif time:%ld ms\n", dif_time);
return 0;
}
Linux下获取毫秒级时间差
代码如下: