结果不好:时间(NULL)和时钟()

Cro*_*ity 5 c macos time clock

#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();
}
Run Code Online (Sandbox Code Playgroud)

当我执行上面的代码时,我得到的结果如下:

时钟总时间:0.0108240

总时间:12

我想让clock()代表一个尽可能接近时间的数字.

上面给出的总时间是在macbook上完成的,但是,我的笔记本电脑(windows)上的代码效果非常好.

CLOCKS_PER_SECOND宏在PC上返回1000,在MAC上返回1,000,000.

nos*_*nos 8

Windows上的clock()返回挂钟时间.*nixes上的clock()返回你的程序花费的CPU时间,这不会很多,你在这里做I/O时可能会被阻止.