Question

I am using timeval as well as clock() function to see the time difference between 2 actions in my c program. Somehow timeval seems to give me the right amount of elapsed time in milliseconds where clock() gives very very less value.

    g_time = clock();
    gettimeofday(&decode_t,NULL);

after sometime

    delay =((float)(clock()-g_time)/(float)CLOCKS_PER_SEC);
    gettimeofday(&poll_t,NULL);
    delay1 = ((poll_t.tv_sec - decode_t.tv_sec)*1000 + (poll_t.tv_usec -  decode_t.tv_usec)/1000.0)    ;
    printf("\ndelay1: %f delay: %f ",delay1,delay);

usual output is:

delay1: 1577.603027 delay: 0.800000

delay1 is in milliseconds and delay is in sec.

Iam using archlinux 64 bit.I can't understand why this is happening.

Was it helpful?

Solution

From the clock(3) manual page:

The clock() function returns an approximation of processor time used by the program.

So the clock function doesn't return the amount of time passed, but a number of "ticks" that your program have run. And as you know, in a multi-tasking system your program can be paused at any time to let other programs run.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top