您的位置:首页 > 运维架构 > Linux

linux获取微秒级别时间

2015-09-25 11:09 651 查看
#include <sys/time.h>

#include <unistd.h>

int gettimeofday(struct timeval *tv,struct timezone *tz);

功能:将目前的时间以tv所指的结构返回。

struct timeval{

long tv_sec;//秒

long tv_usec;//微秒

};

//获取毫秒时间

#include <stdio.h>

#include <stdlib.h> /* 包含标准库头文件 */

#include <sys/time.h>

int main(int argc, char **argv)

{

struct timeval start,stop,diff;

gettimeofday(&start,0); //开始计时

............ //doing

gettimeofday(&stop,0); //结束计时

timeval_subtract(&diff,&start,&stop);

printf("总计用时:%d毫秒/n",diff.tv_usec);

}

//获取微秒级时间

#include <stdlib.h>

#include <stdio.h>

#include <sys/time.h>

int main()

{

struct timeval dwStart;

struct timeval dwEnd;

unsigned long dwTime=0;

int i=0,j=0;

gettimeofday(&dwStart,NULL);

for(i=0;i<100000;i++)

{

;

}

gettimeofday(&dwEnd,NULL);

dwTime = 1000000*(dwEnd.tv_sec-dwStart.tv_sec)+(dwEnd.tv_usec-dwStart.tv_usec);

printf("%ld/n",dwTime);

return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: