新闻中心

EEPW首页 > 嵌入式系统 > 设计应用 > Linux系统下精确到微秒级的时间操作函数

Linux系统下精确到微秒级的时间操作函数

作者: 时间:2016-10-08 来源:网络 收藏

Linux下对时间进行运算,如果是到秒级的,相信大家都用过time之类的函数实现了,但要更精确些呢?到毫秒、微秒级呢?

本文引用地址:https://www.eepw.com.cn/article/201610/305724.htm

看看下面这段源代码就明白了:

#include

#include

#include

void function()/*用来耗用一定的时间而已,无实际用处的函数*/

{

unsigned int i,j;

double y;

for(i=0;i10000;i++)

for(j=0;j10000;j++)

y=sin((double)i);

}

int main(int argc, char ** argv)

{

struct timeval tpstart,tpend;

float timeuse;

gettimeofday(tpstart,NULL);

function();

gettimeofday(tpend,NULL);

timeuse=1000000*(tpend.tv_sec-tpstart.tv_sec)+tpend.tv_usec-tpstart.tv_usec;

timeuse/=1000000;

printf(Used Time:%fn,timeuse);

exit(0);

}

主要是用到了gettimeofday函数,函数里用到了这个结构:

struct timeval {

long tv_sec; /* seconds */

long tv_usec; /* microseconds */

};



关键词:

评论


相关推荐

技术专区

关闭