vhacdTimer.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /* Copyright (c) 2011 Khaled Mamou (kmamou at gmail dot com)
  2. All rights reserved.
  3. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
  4. 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  5. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  6. 3. The names of the contributors may not be used to endorse or promote products derived from this software without specific prior written permission.
  7. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  8. */
  9. #pragma once
  10. #ifndef VHACD_TIMER_H
  11. #define VHACD_TIMER_H
  12. #ifdef _WIN32
  13. #ifndef WIN32_LEAN_AND_MEAN
  14. #define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
  15. #endif
  16. #include <windows.h>
  17. #elif __MACH__
  18. #include <mach/clock.h>
  19. #include <mach/mach.h>
  20. #else
  21. #include <sys/time.h>
  22. #include <time.h>
  23. #endif
  24. namespace VHACD {
  25. #ifdef _WIN32
  26. class Timer {
  27. public:
  28. Timer(void)
  29. {
  30. m_start.QuadPart = 0;
  31. m_stop.QuadPart = 0;
  32. QueryPerformanceFrequency(&m_freq);
  33. };
  34. ~Timer(void){};
  35. void Tic()
  36. {
  37. QueryPerformanceCounter(&m_start);
  38. }
  39. void Toc()
  40. {
  41. QueryPerformanceCounter(&m_stop);
  42. }
  43. double GetElapsedTime() // in ms
  44. {
  45. LARGE_INTEGER delta;
  46. delta.QuadPart = m_stop.QuadPart - m_start.QuadPart;
  47. return (1000.0 * delta.QuadPart) / (double)m_freq.QuadPart;
  48. }
  49. private:
  50. LARGE_INTEGER m_start;
  51. LARGE_INTEGER m_stop;
  52. LARGE_INTEGER m_freq;
  53. };
  54. #elif __MACH__
  55. class Timer {
  56. public:
  57. Timer(void)
  58. {
  59. memset(this, 0, sizeof(Timer));
  60. host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &m_cclock);
  61. };
  62. ~Timer(void)
  63. {
  64. mach_port_deallocate(mach_task_self(), m_cclock);
  65. };
  66. void Tic()
  67. {
  68. clock_get_time(m_cclock, &m_start);
  69. }
  70. void Toc()
  71. {
  72. clock_get_time(m_cclock, &m_stop);
  73. }
  74. double GetElapsedTime() // in ms
  75. {
  76. return 1000.0 * (m_stop.tv_sec - m_start.tv_sec + (1.0E-9) * (m_stop.tv_nsec - m_start.tv_nsec));
  77. }
  78. private:
  79. clock_serv_t m_cclock;
  80. mach_timespec_t m_start;
  81. mach_timespec_t m_stop;
  82. };
  83. #else
  84. class Timer {
  85. public:
  86. Timer(void)
  87. {
  88. memset(this, 0, sizeof(Timer));
  89. };
  90. ~Timer(void){};
  91. void Tic()
  92. {
  93. clock_gettime(CLOCK_REALTIME, &m_start);
  94. }
  95. void Toc()
  96. {
  97. clock_gettime(CLOCK_REALTIME, &m_stop);
  98. }
  99. double GetElapsedTime() // in ms
  100. {
  101. return 1000.0 * (m_stop.tv_sec - m_start.tv_sec + (1.0E-9) * (m_stop.tv_nsec - m_start.tv_nsec));
  102. }
  103. private:
  104. struct timespec m_start;
  105. struct timespec m_stop;
  106. };
  107. #endif
  108. }
  109. #endif // VHACD_TIMER_H