timer.h 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /*
  2. * Copyright 2010-2013 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_NACL || BX_PLATFORM_LINUX || BX_PLATFORM_OSX || BX_PLATFORM_IOS || BX_PLATFORM_QNX
  11. # include <sys/time.h> // gettimeofday
  12. #elif BX_PLATFORM_WINDOWS
  13. # include <windows.h>
  14. #endif // BX_PLATFORM_
  15. namespace bx
  16. {
  17. inline int64_t getHPCounter()
  18. {
  19. #if BX_PLATFORM_WINDOWS || BX_PLATFORM_XBOX360
  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_EMSCRIPTEN
  26. int64_t i64 = clock();
  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. #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. inline int64_t getHPFrequency()
  39. {
  40. #if BX_PLATFORM_WINDOWS || BX_PLATFORM_XBOX360
  41. LARGE_INTEGER li;
  42. QueryPerformanceFrequency(&li);
  43. return li.QuadPart;
  44. #elif BX_PLATFORM_EMSCRIPTEN
  45. return CLOCKS_PER_SEC;
  46. #elif BX_PLATFORM_ANDROID
  47. return INT64_C(1000000000);
  48. #else
  49. return INT64_C(1000000);
  50. #endif // BX_PLATFORM_
  51. }
  52. } // namespace bx
  53. #endif // BX_TIMER_H_HEADER_GUARD