/* Compile with:  gcc -Wall -o timediff2 timediff2.c -lrt */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/time.h>
#include <time.h>

int main()
  {
  struct timespec t1, t2, clock_resolution;
  long long elapsedTime;

  clock_getres(CLOCK_REALTIME, &clock_resolution);
  printf("Resolution  von CLOCK_REALTIME ist %ld Sekunden, %ld Nanosekunden\n",
       clock_resolution.tv_sec, clock_resolution.tv_nsec);

  /* Startwert */
  clock_gettime(CLOCK_REALTIME, &t1);

  /* machwas */
  sleep(1);

  /* Endwert */
  clock_gettime(CLOCK_REALTIME, &t2);

  /* Berechne die verbrauchte Zeit in Nanosekunden */
  elapsedTime = ((t2.tv_sec * 1000000000L) + t2.tv_nsec)
              - ((t1.tv_sec * 1000000000L) + t1.tv_nsec);
  printf("Aufruf dauerte  %lld ns\n", elapsedTime);

  return 0;
  }

