c - Why are values I obtain from clock() unsynchronized with real time? -


i'm using function clock() in c seconds through ticks little test program bellow figured out processor hasn't being counting ticks correctly, because seconds unsynchronized real time , in addition had multiply result 100 more similar seconds think doesnt make sense. in program, 10s equivalent of 7s in real life. me become clock() function bit more precise?

i'm using beaglebone black rev c kernel 3.8.13-bone70, debian 4.6.3-14 , gcc version 4.6.3

thanks in advance!

here's test program:

#include <stdio.h> #include <time.h> int main(){      while(1)     printf("\n%f", (double)100*clock()/clocks_per_sec);      return 0; } 

the result returned clock() function isn't expected synchronized real time. returns

the implementation’s best approximation processor time used program since beginning of implementation-defined era related program invocation

if want high-precision indication of real (wall-clock) time, you'll need use system-specific function such gettimeofday() or clock_gettime() -- or, if implementation supports it, standard timespec_get function, added in c11.

your program calls printf in loop. i'd expect spend of time waiting i/o, yielding other processes, , cpu time indicated clock() (when converted seconds) advance more real time. suspect multiplication 100 throwing off results; clock()/clocks_per_sec should correct indication of cpu time.