0

I have a timer function (for polling) that I want to call every 1uS but I also want to make sure that there is enough time for other tasks to run so I want to measure the time it takes to execute this function. For that I thought I could use clock_gettime() at the beginning of my function and at the very end. Now my problem is, that the two times returned are always the same and I just don't believe that my function runs through in less than 1 nanosecond - it's not possible period. The CPU is clocked at 800MHz. My code:

struct timespec tempTime_start;
struct timespec tempTime_stop;
static int print = 0;
if(clock_gettime(CLOCK_REALTIME, &tempTime_start))
    printf("Error in clock_gettime(): %s",strerror(errno));
    ...
    ...MY FUNCTION...
int i = 0;
for (i = 0;i<100000;i++);
if(clock_gettime(CLOCK_REALTIME, &tempTime_stop))
    printf("Error in clock_gettime(): %s",strerror(errno));
if (print <= 100){
    printf("%dDuration: %d\n",print,(tempTime_stop.tv_nsec - tempTime_start.tv_nsec));
    printf("Start: %d\n",tempTime_start.tv_nsec);
    printf("Stop:  %d\n",tempTime_stop.tv_nsec);
    print++;
}

What am I doing wrong? Start and Stop time are always the same...

stdcerr
  • 13,725
  • 25
  • 71
  • 128

1 Answers1

1

clog_gettime is driven off the system clock. The tv_nsec member are pretty meaningless.
The resolution will be anywhere between 60 Hz and 4KHz.

If you want to do timestamping, you should use the sysTimestamp functionality.
This often uses a decrementer counter (in a PPC architecture) which does provide ns accuracy.

However, your system must be configured to use it.

Benoit
  • 37,894
  • 24
  • 81
  • 116