Linux下获取毫秒级时间差

来源:互联网 发布:网络暴力赚钱 编辑:程序博客网 时间:2024/06/07 06:08
代码如下:

#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;

}


原创粉丝点击