Linux下对时间进行运算,如果是到秒级的,相信大家都用过time之类的函数实现了,但要更精确些呢?到毫秒、微秒级呢?
看看下面这段源代码就明白了:
#include <sys/time.h>
#include <stdio.h>
#include <math.h>
void function()/*用来耗用一定的时间而已,无实际用处的函数*/
{
unsigned int i,j;
double y;
for(i=0;i<10000;i++)
for(j=0;j<10000;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:%f\n",timeuse);
exit(0);
}
|
主要是用到了gettimeofday函数,函数里用到了这个结构:
struct timeval {
long tv_sec; /* seconds */
long tv_usec; /* microseconds */
};
|
(责任编辑:A6)
本站文章仅代表作者观点,本站仅传递信息,并不表示赞同或反对.转载本站点内容时请注明来自www.linuxeden.com-Linux伊甸园。如不注明,www.linuxeden.com将根据《互联网著作权行政保护办法》追究其相应法律责任。
来源:赛迪网 作者:sixth 时间: 2008-04-21 11:00:30 评论0条 点击:阅读次数:
次
把开源带在你的身边-精美linux小纪念品
评论加载中…