리눅스에서 나노세컨드 단위의 시간측정은 어떻게 하나요?

유니1944의 이미지

윈도우의 경우 QueryPerformance QueryPerformanceCounter 라는 함수를 가지고

비교적 정확하게 측정할수있는데요(여러 프로그램을 돌리지 않는다고 가정할대..)

리눅스는 어떻게 측정해야 할지..
??

익명 사용자의 이미지

#include <time.h>
/*
   struct timespec {
      time_t   tv_sec;        // seconds
      long     tv_nsec;       // nanoseconds
   };
 */

main()
{
   struct timespec tp;
   int rs;

   rs = clock_gettime(CLOCK_REALTIME, &tp);
   printf("%ld %ld\n", tp.tv_sec, tp.tv_nsec);
   rs = clock_gettime(CLOCK_REALTIME, &tp);
   printf("%ld %ld\n", tp.tv_sec, tp.tv_nsec);
   return 0;
}

$ gcc -o test test.c -lrt