timer.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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 || BX_PLATFORM_XBOX360 || BX_PLATFORM_XBOXONE || BX_PLATFORM_WINRT
  20. LARGE_INTEGER li;
  21. // Performance counter value may unexpectedly leap forward
  22. // http://support.microsoft.com/kb/274323
  23. QueryPerformanceCounter(&li);
  24. int64_t i64 = li.QuadPart;
  25. #elif BX_PLATFORM_ANDROID
  26. struct timespec now;
  27. clock_gettime(CLOCK_MONOTONIC, &now);
  28. int64_t i64 = now.tv_sec*INT64_C(1000000000) + now.tv_nsec;
  29. #elif BX_PLATFORM_EMSCRIPTEN
  30. int64_t i64 = int64_t(1000.0f * emscripten_get_now() );
  31. #else
  32. struct timeval now;
  33. gettimeofday(&now, 0);
  34. int64_t i64 = now.tv_sec*INT64_C(1000000) + now.tv_usec;
  35. #endif // BX_PLATFORM_
  36. return i64;
  37. }
  38. int64_t getHPFrequency()
  39. {
  40. #if BX_PLATFORM_WINDOWS || BX_PLATFORM_XBOX360 || BX_PLATFORM_XBOXONE || BX_PLATFORM_WINRT
  41. LARGE_INTEGER li;
  42. QueryPerformanceFrequency(&li);
  43. return li.QuadPart;
  44. #elif BX_PLATFORM_ANDROID
  45. return INT64_C(1000000000);
  46. #elif BX_PLATFORM_EMSCRIPTEN
  47. return INT64_C(1000000);
  48. #else
  49. return INT64_C(1000000);
  50. #endif // BX_PLATFORM_
  51. }
  52. } // namespace bx