timer.h 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*
  2. * Copyright 2010-2015 Branimir Karadzic. All rights reserved.
  3. * License: http://www.opensource.org/licenses/BSD-2-Clause
  4. */
  5. #ifndef BX_TIMER_H_HEADER_GUARD
  6. #define BX_TIMER_H_HEADER_GUARD
  7. #include "bx.h"
  8. #if BX_PLATFORM_ANDROID
  9. # include <time.h> // clock, clock_gettime
  10. #elif BX_PLATFORM_EMSCRIPTEN
  11. # include <emscripten.h>
  12. #elif BX_PLATFORM_WINDOWS || BX_PLATFORM_WINRT
  13. # include <windows.h>
  14. #else
  15. # include <sys/time.h> // gettimeofday
  16. #endif // BX_PLATFORM_
  17. namespace bx
  18. {
  19. inline int64_t getHPCounter()
  20. {
  21. #if BX_PLATFORM_WINDOWS || BX_PLATFORM_XBOX360 || 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. #else
  34. struct timeval now;
  35. gettimeofday(&now, 0);
  36. int64_t i64 = now.tv_sec*INT64_C(1000000) + now.tv_usec;
  37. #endif // BX_PLATFORM_
  38. return i64;
  39. }
  40. inline int64_t getHPFrequency()
  41. {
  42. #if BX_PLATFORM_WINDOWS || BX_PLATFORM_XBOX360 || BX_PLATFORM_WINRT
  43. LARGE_INTEGER li;
  44. QueryPerformanceFrequency(&li);
  45. return li.QuadPart;
  46. #elif BX_PLATFORM_ANDROID
  47. return INT64_C(1000000000);
  48. #elif BX_PLATFORM_EMSCRIPTEN
  49. return INT64_C(1000000);
  50. #else
  51. return INT64_C(1000000);
  52. #endif // BX_PLATFORM_
  53. }
  54. } // namespace bx
  55. #endif // BX_TIMER_H_HEADER_GUARD