Wednesday, April 6, 2011

Minimum resolution of the C 'clock' function

Here is the C program to find out the minimum resolution of the 'clock' function on your system.

#include <time.h>
#include <stdio.h>

int main(int argc, char** argv) {
    clock_t a, b;
    a = b = clock();
    while(a == b) {
        b = clock();
    }
    printf("Minimum resolution by 'clock()' = %lf s\n",
           ((double) (b - a))/CLOCKS_PER_SEC);
    return 0;
}

How to compile? (assuming that you have stored this program in a file named 'minRes.c')
gcc -o minRes minRes.c

How to run?
minRes

On my system (Dell Latitude E6400, running Win7), I got this to be about 15ms, on an average.
What about you?

1 comment:

  1. /*

    The code only gives right result when I don't put "a" to subtract "b" first before devide it by "CLOCKS_PER_SEC" ..
    And the result that I got with that way is exactly right as you've got ..

    Despite it all, . ..
    There's something that I can't understand.
    Why the return value of "CLOCKS_PER_SEC" is always 1000, whereas my system show CPU has 3.06 GHz in clockspeed .??!

    */

    ReplyDelete