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...