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.

51 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. #else
  31. #include <time.h>
  32. class SqTime
  33. {
  34. public:
  35. static double seconds()
  36. {
  37. struct timespec ts;
  38. int x = clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts);
  39. assert(x == 0);
  40. // seconds = sec + nsec / 10**9
  41. return double(ts.tv_sec) + double(ts.tv_nsec) / (1000.0 * 1000.0 * 1000.0);
  42. }
  43. };
  44. #endif