o3dgcTimer.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. Copyright (c) 2013 Khaled Mammou - Advanced Micro Devices, Inc.
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #pragma once
  20. #ifndef O3DGC_TIMER_H
  21. #define O3DGC_TIMER_H
  22. #include "o3dgcCommon.h"
  23. #ifdef _WIN32
  24. /* Thank you, Microsoft, for file WinDef.h with min/max redefinition. */
  25. #define NOMINMAX
  26. #include <windows.h>
  27. #elif __MACH__
  28. #include <mach/clock.h>
  29. #include <mach/mach.h>
  30. #else
  31. #include <time.h>
  32. #include <sys/time.h>
  33. #endif
  34. namespace o3dgc
  35. {
  36. #ifdef _WIN32
  37. class Timer
  38. {
  39. public:
  40. Timer(void)
  41. {
  42. m_start.QuadPart = 0;
  43. m_stop.QuadPart = 0;
  44. QueryPerformanceFrequency( &m_freq ) ;
  45. };
  46. ~Timer(void){};
  47. void Tic()
  48. {
  49. QueryPerformanceCounter(&m_start) ;
  50. }
  51. void Toc()
  52. {
  53. QueryPerformanceCounter(&m_stop);
  54. }
  55. double GetElapsedTime() // in ms
  56. {
  57. LARGE_INTEGER delta;
  58. delta.QuadPart = m_stop.QuadPart - m_start.QuadPart;
  59. return (1000.0 * delta.QuadPart) / (double)m_freq.QuadPart;
  60. }
  61. private:
  62. LARGE_INTEGER m_start;
  63. LARGE_INTEGER m_stop;
  64. LARGE_INTEGER m_freq;
  65. };
  66. #elif __MACH__
  67. class Timer
  68. {
  69. public:
  70. Timer(void)
  71. {
  72. memset(this, 0, sizeof(Timer));
  73. host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, & m_cclock);
  74. };
  75. ~Timer(void)
  76. {
  77. mach_port_deallocate(mach_task_self(), m_cclock);
  78. };
  79. void Tic()
  80. {
  81. clock_get_time( m_cclock, &m_start);
  82. }
  83. void Toc()
  84. {
  85. clock_get_time( m_cclock, &m_stop);
  86. }
  87. double GetElapsedTime() // in ms
  88. {
  89. return 1000.0 * (m_stop.tv_sec - m_start.tv_sec + (1.0E-9) * (m_stop.tv_nsec - m_start.tv_nsec));
  90. }
  91. private:
  92. clock_serv_t m_cclock;
  93. mach_timespec_t m_start;
  94. mach_timespec_t m_stop;
  95. };
  96. #else
  97. class Timer
  98. {
  99. public:
  100. Timer(void)
  101. {
  102. memset(this, 0, sizeof(Timer));
  103. };
  104. ~Timer(void){};
  105. void Tic()
  106. {
  107. clock_gettime(CLOCK_REALTIME, &m_start);
  108. }
  109. void Toc()
  110. {
  111. clock_gettime(CLOCK_REALTIME, &m_stop);
  112. }
  113. double GetElapsedTime() // in ms
  114. {
  115. return 1000.0 * (m_stop.tv_sec - m_start.tv_sec + (1.0E-9) * (m_stop.tv_nsec - m_start.tv_nsec));
  116. }
  117. private:
  118. struct timespec m_start;
  119. struct timespec m_stop;
  120. };
  121. #endif
  122. }
  123. #endif // O3DGC_TIMER_H