o3dgcTimer.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. #ifndef NOMINMAX
  26. #define NOMINMAX
  27. #endif
  28. #include <windows.h>
  29. #elif __APPLE__
  30. #include <mach/clock.h>
  31. #include <mach/mach.h>
  32. #else
  33. #include <time.h>
  34. #include <sys/time.h>
  35. #endif
  36. namespace o3dgc
  37. {
  38. #ifdef _WIN32
  39. class Timer
  40. {
  41. public:
  42. Timer(void)
  43. {
  44. m_start.QuadPart = 0;
  45. m_stop.QuadPart = 0;
  46. QueryPerformanceFrequency( &m_freq ) ;
  47. };
  48. ~Timer(void){};
  49. void Tic()
  50. {
  51. QueryPerformanceCounter(&m_start) ;
  52. }
  53. void Toc()
  54. {
  55. QueryPerformanceCounter(&m_stop);
  56. }
  57. double GetElapsedTime() // in ms
  58. {
  59. LARGE_INTEGER delta;
  60. delta.QuadPart = m_stop.QuadPart - m_start.QuadPart;
  61. return (1000.0 * delta.QuadPart) / (double)m_freq.QuadPart;
  62. }
  63. private:
  64. LARGE_INTEGER m_start;
  65. LARGE_INTEGER m_stop;
  66. LARGE_INTEGER m_freq;
  67. };
  68. #elif __APPLE__
  69. class Timer
  70. {
  71. public:
  72. Timer(void)
  73. {
  74. memset(this, 0, sizeof(Timer));
  75. host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, & m_cclock);
  76. };
  77. ~Timer(void)
  78. {
  79. mach_port_deallocate(mach_task_self(), m_cclock);
  80. };
  81. void Tic()
  82. {
  83. clock_get_time( m_cclock, &m_start);
  84. }
  85. void Toc()
  86. {
  87. clock_get_time( m_cclock, &m_stop);
  88. }
  89. double GetElapsedTime() // in ms
  90. {
  91. return 1000.0 * (m_stop.tv_sec - m_start.tv_sec + (1.0E-9) * (m_stop.tv_nsec - m_start.tv_nsec));
  92. }
  93. private:
  94. clock_serv_t m_cclock;
  95. mach_timespec_t m_start;
  96. mach_timespec_t m_stop;
  97. };
  98. #else
  99. class Timer
  100. {
  101. public:
  102. Timer(void)
  103. {
  104. memset(this, 0, sizeof(Timer));
  105. };
  106. void Tic()
  107. {
  108. clock_gettime(CLOCK_REALTIME, &m_start);
  109. }
  110. void Toc()
  111. {
  112. clock_gettime(CLOCK_REALTIME, &m_stop);
  113. }
  114. double GetElapsedTime() // in ms
  115. {
  116. return 1000.0 * (m_stop.tv_sec - m_start.tv_sec + (1.0E-9) * (m_stop.tv_nsec - m_start.tv_nsec));
  117. }
  118. private:
  119. struct timespec m_start;
  120. struct timespec m_stop;
  121. };
  122. #endif
  123. }
  124. #endif // O3DGC_TIMER_H