o3dgcTimer.h 3.4 KB

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