Bad Results: time(NULL) and 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();
}

When I execute the above code I get results like:

Total Time by Clock: 0.0108240

Total Time by Time: 12

I would like to have clock() represent a number as close to as possible as time.

The total time presented above was done on a macbook, however, the code works excellent on my laptop (windows).

The CLOCKS_PER_SECOND macro returns 1000 on the PC, 1,000,000 on the MAC.

5
задан Vadim Kotov 5 September 2017 в 13:29
поделиться