smallest resolution using gettimeofday() in C

Hello ...
Can somebody help telling me how smallest clock resolution on ones sytem can be in obtained in C using gettimeofday() .

Thankz in advance...

The smallest resolution for gettimeofday would be microseconds according to the manpage.

1 microsecond is the granularity of the system call, not the resolution. To obtain the resolution you should use clock_getres() if you have it. Unix systems without clock_getres() tend to have a resolution of 10 milliseconds. There is no reliable way to obtain the resolution with gettimeofday().

gettimeofday will give you the current date and time specified in seconds or microseconds from the UNIX Epoch but not the resolution of the system-wide clock so be specific as to what you want?

/*

  • Print the gettimeofday() clock resolution for this machine.
    *
  • Code taken from Iozone. Iozone Filesystem Benchmark
  • Author: Don Capps
    *
    /
    #include <sys/time.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/types.h>
    #define THISVERSION " Version $Revision: 1.2 $"
    double time_res,delay;
    void get_resolution(); /
    Works with most compilers /
    static double time_so_far1(); /
    Works with most compilers /
    /
  • Measure and print the gettimeofday() resolution.
    /
    main()
    {
    printf("\n\nMeasuring the gettimeofday() resolution\n\n");
    get_resolution();
    printf("Time resolution of gettimeofday() = %f seconds\n",time_res);
    printf("Time resolution of gettimeofday() = %f milli seconds\n",
    time_res
    1000);
    printf("Time resolution of gettimeofday() = %f micro seconds\n\n",
    time_res*(10001000));
    printf("Provided, courtesy of Iozone. http://www.iozone.org\n\n");
    }
    /
  • Lifted code from Iozone.
    /
    #ifdef HAVE_ANSIC_C
    void
    get_resolution(void)
    #else
    void
    get_resolution()
    #endif
    {
    double starttime, finishtime;
    long j;
    again:
    finishtime=time_so_far1(); /
    Warm up the instruction cache /
    starttime=time_so_far1(); /
    Warm up the instruction cache /
    delay=j=0; /
    Warm up the data cache /
    while(1)
    {
    starttime=time_so_far1();
    for(j=0;j< delay;j++)
    ;
    finishtime=time_so_far1();
    if(starttime==finishtime)
    delay++;
    else
    break;
    }
    time_res = (finishtime-starttime)/1000000.0;
    }
    /
  • Lifted code from Iozone.
    /
    /
    /
    /
    Time measurement routines. /
    /
    Return time in microseconds /
    /
    /
    #ifdef HAVE_ANSIC_C
    static double
    time_so_far1(void)
    #else
    static double
    time_so_far1()
    #endif
    {
    /
    For Windows the time_of_day() is useless. It increments in 55 milli second /
    /
    increments. By using the Win32api one can get access to the high performance /
    /
    measurement interfaces. With this one can get back into the 8 to 9 /
    /
    microsecond resolution. */
    #ifdef Windows
    LARGE_INTEGER freq,counter;
    double wintime;
    double bigcounter;
    QueryPerformanceFrequency(&freq);
    QueryPerformanceCounter(&counter);
    bigcounter=(double)counter.HighPart (double)0xffffffff +
    (double)counter.LowPart;
    wintime = (double)(bigcounter/(double)freq.LowPart);
    return((double)wintime
    1000000.0);
    #else
    #if defined (OSFV4) || defined(OSFV3) || defined(OSFV5)
    struct timespec gp;
    if (getclock(TIMEOFDAY, (struct timespec *) &gp) == -1)
    perror("getclock");
    return (( (double) (gp.tv_sec)*1000000.0) +
    ( ((float)(gp.tv_nsec)) * 0.001 ));
    #else
    struct timeval tp;
    if (gettimeofday(&tp, (struct timezone *) NULL) == -1)
    perror("gettimeofday");
    return ((double) (tp.tv_sec)*1000000.0) +
    (((double) tp.tv_usec) );
    #endif
    #endif
    }