timer.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*
  2. * Copyright 2010-2017 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bx#license-bsd-2-clause
  4. */
  5. #include <bx/timer.h>
  6. #if BX_PLATFORM_ANDROID
  7. # include <time.h> // clock, clock_gettime
  8. #elif BX_PLATFORM_EMSCRIPTEN
  9. # include <emscripten.h>
  10. #elif BX_PLATFORM_WINDOWS || BX_PLATFORM_XBOXONE || BX_PLATFORM_WINRT
  11. # include <windows.h>
  12. #else
  13. # include <sys/time.h> // gettimeofday
  14. #endif // BX_PLATFORM_
  15. namespace bx
  16. {
  17. int64_t getHPCounter()
  18. {
  19. #if BX_PLATFORM_WINDOWS \
  20. || BX_PLATFORM_XBOXONE \
  21. || BX_PLATFORM_WINRT
  22. LARGE_INTEGER li;
  23. // Performance counter value may unexpectedly leap forward
  24. // http://support.microsoft.com/kb/274323
  25. QueryPerformanceCounter(&li);
  26. int64_t i64 = li.QuadPart;
  27. #elif BX_PLATFORM_ANDROID
  28. struct timespec now;
  29. clock_gettime(CLOCK_MONOTONIC, &now);
  30. int64_t i64 = now.tv_sec*INT64_C(1000000000) + now.tv_nsec;
  31. #elif BX_PLATFORM_EMSCRIPTEN
  32. int64_t i64 = int64_t(1000.0f * emscripten_get_now() );
  33. #elif !BX_PLATFORM_NONE
  34. struct timeval now;
  35. gettimeofday(&now, 0);
  36. int64_t i64 = now.tv_sec*INT64_C(1000000) + now.tv_usec;
  37. #else
  38. BX_CHECK(false, "Not implemented!");
  39. int64_t i64 = UINT64_MAX;
  40. #endif // BX_PLATFORM_
  41. return i64;
  42. }
  43. int64_t getHPFrequency()
  44. {
  45. #if BX_PLATFORM_WINDOWS \
  46. || BX_PLATFORM_XBOXONE \
  47. || BX_PLATFORM_WINRT
  48. LARGE_INTEGER li;
  49. QueryPerformanceFrequency(&li);
  50. return li.QuadPart;
  51. #elif BX_PLATFORM_ANDROID
  52. return INT64_C(1000000000);
  53. #elif BX_PLATFORM_EMSCRIPTEN
  54. return INT64_C(1000000);
  55. #else
  56. return INT64_C(1000000);
  57. #endif // BX_PLATFORM_
  58. }
  59. } // namespace bx