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.

52 lines
1.0KB

  1. #pragma once
  2. /**
  3. * @class SqTime
  4. * A simple high-res timer. Returns current seconds
  5. */
  6. /**
  7. * Windows version is based on QueryPerformanceFrequency
  8. * Typically this is accurate to 1/3 microsecond. Can be made more
  9. * accurate by tinkering with your bios
  10. */
  11. #if defined(_MSC_VER) || defined(ARCH_WIN)
  12. #include <Windows.h>
  13. class SqTime
  14. {
  15. public:
  16. static double seconds()
  17. {
  18. LARGE_INTEGER t;
  19. if (frequency == 0) {
  20. QueryPerformanceFrequency(&t);
  21. frequency = double(t.QuadPart);
  22. }
  23. QueryPerformanceCounter(&t);
  24. int64_t n = t.QuadPart;
  25. return double(n) / frequency;
  26. }
  27. private:
  28. static double frequency;
  29. };
  30. double SqTime::frequency = 0;
  31. #else
  32. #include <time.h>
  33. class SqTime
  34. {
  35. public:
  36. static double seconds()
  37. {
  38. struct timespec ts;
  39. int x = clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts);
  40. assert(x == 0);
  41. // seconds = sec + nsec / 10**9
  42. return double(ts.tv_sec) + double(ts.tv_nsec) / (1000.0 * 1000.0 * 1000.0);
  43. }
  44. };
  45. #endif