timer.h 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. * Copyright 2010-2011 Branimir Karadzic. All rights reserved.
  3. * License: http://www.opensource.org/licenses/BSD-2-Clause
  4. */
  5. #ifndef __BX_TIMER_H__
  6. #define __BX_TIMER_H__
  7. #include "bx.h"
  8. #if BX_PLATFORM_ANDROID
  9. # include <time.h> // clock, clock_gettime
  10. #elif BX_PLATFORM_NACL | BX_PLATFORM_LINUX
  11. # include <sys/time.h> // gettimeofday
  12. #elif BX_PLATFORM_OSX
  13. # include <mach/mach_time.h> // mach_absolute_time/mach_timebase_info
  14. #elif BX_PLATFORM_WINDOWS
  15. # include <windows.h>
  16. #endif // BX_PLATFORM_
  17. namespace bx
  18. {
  19. inline int64_t getHPCounter()
  20. {
  21. #if BX_PLATFORM_WINDOWS || BX_PLATFORM_XBOX360
  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. int64_t i64 = clock();
  29. #elif BX_PLATFORM_OSX
  30. int64_t i64 = mach_absolute_time();
  31. #else
  32. struct timeval now;
  33. gettimeofday(&now, 0);
  34. int64_t i64 = now.tv_sec*1000000 + now.tv_usec;
  35. #endif // BNET_PLATFORM_
  36. static int64_t offset = i64;
  37. return i64 - offset;
  38. }
  39. inline int64_t getHPFrequency()
  40. {
  41. #if BX_PLATFORM_WINDOWS || BX_PLATFORM_XBOX360
  42. LARGE_INTEGER li;
  43. QueryPerformanceFrequency(&li);
  44. return li.QuadPart;
  45. #elif BX_PLATFORM_ANDROID
  46. return CLOCKS_PER_SEC;
  47. #elif BX_PLATFORM_OSX
  48. mach_timebase_info_data_t info;
  49. mach_timebase_info(&info);
  50. return (int64_t)(info.denom * 1000000) / info.numer;
  51. #else
  52. return 1000000;
  53. #endif // BNET_PLATFORM_
  54. }
  55. } // namespace bx
  56. #endif // __BX_TIMER_H__