Inaccurate timing - Time.h issue

Dear All,

I was experimenting with the timing and timing accuracy on BBB. and I came across a very interesting yet strange thing!

Just running clock_gettime()twice and measuring the time difference, gives you horrendous timing. Here is my simple C code:

`
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

int main()
{

struct timeval startTime, stopTime;

clock_gettime(CLOCK_REALTIME, &sT);
printf("\n%ld.%09ld\n" ,sT.tv_sec,sT.tv_nsec);

clock_gettime(CLOCK_REALTIME, &eT);
printf("\n%ld.%09ld %ld uSec\n" , eT.tv_sec, eT.tv_nsec, ((eT.tv_sec - sT.tv_sec)*1000000 + (eT.tv_nsec-sT.tv_nsec)/1000));

return 0;
}
`

Just two gettime are causing 2894 to 3200 micro sec (uSec) time difference, how is such huge amount possible!??

Can someone throw some light on this? and can it be improved or fixed?

I have tried the following to investigate.

  1. Running same code on my i7
    it gives 80uS to ~300 uS delay.

  2. Trying TimeVal and GetTimeOfDay – gives worse result!

  3. Running NTP:
    No impact.

Thank you, and hope you find it interesting as well :slight_smile:

I suggest you call clock_gettime () twice with nothing between these calls. printf() is a complicated function that may interact with hardware, for example, a serial port or ethernet. The time required to do input or output may depend on the state of the hardware, or on the operation of a queue, or maybe even task switching that happens when the system waits for I/O.

( I don’t see the definition of sT and eT. Are they declred as pointers to startTIme and stopTime ?)

Dear David,

Thanks for the suggestion, IT WORKED!

latency reduced to 4 uSec that’s huge. and yes, I made mistake in pasting the code and removing the comments.

Thanks a ton.

Reposting the code bellow:

`
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

int main()
{

struct timespec sT, eT

clock_gettime(CLOCK_REALTIME, &sT);
// printf("\n%ld.%09ld\n" ,sT.tv_sec,sT.tv_nsec);

clock_gettime(CLOCK_REALTIME, &eT);
printf("\n%ld.%09ld %ld uSec\n" , eT.tv_sec, eT.tv_nsec, ((eT.tv_sec - sT.tv_sec)*1000000 +(eT.tv_nsec-sT.tv_nsec)/1000));

return 0;
}

`

Regards,
Rathin