/src/qtimer/gethrtime.c

https://github.com/Qthreads/qthreads · C · 60 lines · 46 code · 13 blank · 1 comment · 1 complexity · 7f53bf7eb863a50683eaede8d80cf586 MD5 · raw file

  1. #ifdef HAVE_CONFIG_H
  2. # include "config.h"
  3. #endif
  4. #include <stdlib.h>
  5. #include <qthread/qtimer.h>
  6. #include "qt_debug.h" /* for malloc debug wrappers */
  7. struct qtimer_s {
  8. hrtime_t start;
  9. hrtime_t stop;
  10. };
  11. void qtimer_start(qtimer_t q)
  12. {
  13. q->start = gethrtime();
  14. }
  15. unsigned long qtimer_fastrand(void)
  16. {
  17. return (unsigned long)(gethrtime());
  18. }
  19. void qtimer_stop(qtimer_t q)
  20. {
  21. q->stop = gethrtime();
  22. }
  23. double qtimer_wtime(void)
  24. {
  25. return ((double)gethrtime()) * 1e-9;
  26. }
  27. double qtimer_res(void)
  28. {
  29. #if defined(HAVE_SYSCONF) && defined(HAVE_SC_CLK_TCK)
  30. return 1.0 / sysconf(_SC_CLK_TCK);;
  31. #else
  32. return 1e-9;
  33. #endif
  34. }
  35. double qtimer_secs(qtimer_t q)
  36. {
  37. return ((double)(q->stop - q->start)) * 1e-9;
  38. }
  39. qtimer_t qtimer_create()
  40. {
  41. return qt_calloc(1, sizeof(struct qtimer_s));
  42. }
  43. void qtimer_destroy(qtimer_t q)
  44. {
  45. FREE(q, sizeof(struct qtimer_s));
  46. }
  47. /* vim:set expandtab: */