在算法评估中,测量函数的或者程序的执行时间很重要。主要的测量方法有
1.clock()函数,需要包含#include<time.h>,应该是属于C标准库的。精确至s,可做为粗略的估计。
clock_t starttime = clock();
....
clock_t end = clock() ;
float exec_sec = (float)(end - starttime) / CLOCKS_PER_SEC
2 int gettimeofday(struct timeval*tv,struct timezone *tz )函数,需要包含#include<sys/time.h>。
精度较高,微秒级,1m = 1E6 us
struct timeval tstart,tend;
gettimeofday(&tstart,NULL);
.....
gettimeofday(&tend,NULL);
long long exec_usecs = 1000000*(tend.tv_sec - tstart.tv_sec) + (tend.tv_usec - tstart.tv_usec);