计算一个函数调用花费了多长时间。
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
本文介绍了一种利用 gettimeofday 函数来测量特定函数调用所需时间的方法。通过记录调用前后的时间差,可以精确到微秒级别地计算出函数执行消耗的时间。

1513

被折叠的 条评论
为什么被折叠?



