测量函数花费时间

计算一个函数调用花费了多长时间。

int gettimeofday(struct timeval *tv,struct timezone *tz); //tz一般使用NULL

strut timeval {
long tv_sec; /* 秒数 */
long tv_usec; /* 微秒数 */
};

#if OSL_DEBUG_LEVEL >1
timeval aLast;
float timeuse;
gettimeofday(&aLast,NULL);

#endif

function(); //测量此函数执行所花费的时间

#if OSL_DEBUG_LEVEL >1
timeval aNow;

gettimeofday(&aNow,NULL);
timeuse=1000000*(aNow.tv_sec-aLast.tv_sec)+ aNow.tv_usec-aLast.tv_usec;
timeuse/=1000000;
fprintf(stderr,"function() spend time:  sec=%f\n",timeuse);

#endif

 

posted @ 2012-08-16 17:02  MayFirst  阅读(343)  评论(0编辑  收藏  举报