Time.h clock () broken on OS X?

I've gone crazy? I am running this on x86_64.

#include <stdio.h> #include <time.h> #include <unistd.h> int main(int argc, char *argv[]) { printf("Clock: %f\n", clock() / (double)CLOCKS_PER_SEC); sleep(1); printf("Clock: %f\n", clock() / (double)CLOCKS_PER_SEC); sleep(1); printf("Clock: %f\n", clock() / (double)CLOCKS_PER_SEC); return 0; } 

Will print

Hours: 0.002880
Hours: 0.002968
Hours: 0.003019

It explicitly waits for the second line of sleep(1) , but the result is clearly erroneous.

If this does not work, is there a portable alternative to C?

+1
source share
2 answers

I am an idiot. clock() returns the processor time .

+4
source

You can declare a variable time_t t1,t2; t1 = time(NULL); sleep(1); t2 = time(NULL); You can debug and view the values โ€‹โ€‹of t1 and t2. I think you can take the link from http://www.cplusplus.com/reference/clibrary/ctime/time/

0
source

Source: https://habr.com/ru/post/1489999/


All Articles