为方便跨平台使用,推荐写法如下:
#ifdef _WIN
#include <time.h>
double clock_new(){
clock_t time_;
time_ = clock();
double time_d = (double)time_; //单位:ms
return time_d;
}
#else
#include <sys/time.h>
double clock_new(){
timeval time_;
gettimeofday(&time_, NULL);
double time_d = time_.tv_sec * 1000 + time_.tv_usec / 1000;
return time_d;
}
#endif
int main(){
double start, end;
start = clock_new();
...
end = clock_new();
printf("time cost: %.3f ms\n", end - start);
return 0;
}
参考链接: