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

王朝system·作者佚名  2008-06-01
窄屏简体版  字體: |||超大  

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 */

};

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航