void test() { } int main() { timeval tStart,cTime; int i = 0; long long tPassed = 0; gettimeofday(&tStart, 0); #pragma omp parallel for for(i = 0; i < INT_MAX; ++i) { test(); } gettimeofday(&cTime, 0); cTime.tv_sec -= tStart.tv_sec; cTime.tv_usec -= tStart.tv_usec; tPassed = 1000000LL * cTime.tv_sec + cTime.tv_usec; tPassed /= 1000; printf("Time = %lldms\n", tPassed); return 0; }
改为clock后
int main() { clock_t start = clock(), end; int i = 0; #pragma omp parallel for for(i = 0; i < INT_MAX; ++i) { test(); } end = clock(); cout << (end - start) / CLOCKS_PER_SEC * 1000 << "ms" << endl; return 0; }
clock有三个问题:
1)如果超过一个小时,将要导致溢出.
2)函数clock没有考虑CPU被子进程使用的情况.
3)也不能区分用户空间和内核空间.