You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

common.c 923B

12345678910111213141516171819202122232425262728293031323334353637
  1. #include "common.h"
  2. #include <stdio.h>
  3. struct timespec get_time()
  4. {
  5. struct timespec start_time;
  6. clock_gettime(CLOCK_MONOTONIC, &start_time);
  7. return start_time;
  8. }
  9. struct timespec time_diff(struct timespec* ts1, struct timespec* ts2)
  10. {
  11. static struct timespec ts;
  12. ts.tv_sec = ts1->tv_sec - ts2->tv_sec;
  13. ts.tv_nsec = ts1->tv_nsec - ts2->tv_nsec;
  14. if (ts.tv_nsec < 0) {
  15. ts.tv_sec--;
  16. ts.tv_nsec += 1000000000;
  17. }
  18. return ts;
  19. }
  20. struct timespec get_duration(struct timespec* ts)
  21. {
  22. struct timespec time = get_time();
  23. return time_diff(&time, ts);
  24. }
  25. void print_time(struct timespec* ts)
  26. {
  27. long ns = ts->tv_nsec % 1000;
  28. long us = (ts->tv_nsec / 1000) % 1000;
  29. long ms = (ts->tv_nsec / 1000000) % 1000;
  30. long s = (ts->tv_nsec / 1000000000) % 1000 + ts->tv_sec;
  31. long t = (s * 1000000000) + (ms * 1000000) + (us * 1000) + ns;
  32. printf("%3lds %3ldms %3ldus %3ldns %12ld", s, ms, us, ns, t);
  33. }