#import <stdio.h>
#import <time.h>
int main (void) {
printf("Clock ticks per second: %d\n", CLOCKS_PER_SEC);
double check = clock();
int timex = time(NULL);
for (int x = 0; x <= 500000; x++) {
printf(".");
}
puts("\n");
printf("Total Time by Clock: %7.7f\n", (clock() - check) / CLOCKS_PER_SEC );
printf("Total Time by Time: %d\n", time(NULL) - timex);
getchar();
}
当我执行上面的代码时,我得到如下结果:
时钟总时间:0.0108240
每次总时间:12
我想让clock()代表一个尽可能接近时间的数字。
上面显示的总时间是在Macbook上完成的,但是,该代码在我的笔记本电脑(Windows)上效果很好。
CLOCKS_PER_SECOND宏在PC上返回1000,在MAC上返回1,000,000。
最佳答案
Windows上的clock()返回挂钟时间。 * nixes上的clock()返回程序所花费的CPU时间,这不会很多,在这里执行I/O时可能会阻塞。
关于c - 不良结果: time(NULL) and clock(),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5909556/