#include <stdio.h> #include <sys/time.h> #include <time.h> int main(int argc,char * argv[]) { struct timeval tv; while(1) { gettimeofday(&tv,NULL); printf("time %u:%u\n",tv.tv_sec,tv.tv_usec); sleep(2); } return 0; }
调试一个函数执行时间时,可使用两个timeval的变量,分别记录函数起始和函数结束时的时间,从而计算出该函数执行的时间消耗。linux