fork download
  1. #ifndef FURROVINESTOPWATCH_H
  2. #define FURROVINESTOPWATCH_H
  3.  
  4. #include <Furrovine++/Core.h>
  5. #include <Furrovine++/TimeSpan.h>
  6.  
  7. namespace Furrovine {
  8.  
  9. class furrovineexport Stopwatch {
  10. private:
  11. #ifdef FURROVINEWIN
  12. LARGE_INTEGER countstart;
  13. LARGE_INTEGER countend;
  14. LARGE_INTEGER frequency;
  15. #elif FURROVINEAPPLE || FURROVINELINUX
  16. timeval countstart;
  17. timeval countend;
  18. #endif
  19. bool stopped;
  20.  
  21. public:
  22.  
  23. Stopwatch () : stopped(true) {
  24. #ifdef FURROVINEWIN
  25. countstart.QuadPart = 0;
  26. countend.QuadPart = 0;
  27. #elif FURROVINEAPPLE || FURROVINELINUX
  28. countstart.tv_sec = countstart.tv_usec = 0;
  29. countend.tv_sec = countend.tv_usec = 0;
  30. #endif
  31. }
  32.  
  33. void Start () {
  34. stopped = false;
  35. #ifdef FURROVINEWIN
  36. QueryPerformanceCounter(&countstart);
  37. #elif FURROVINEAPPLE || FURROVINELINUX
  38. gettimeofday(&countstart, NULL);
  39. #endif
  40. }
  41.  
  42. void Restart () {
  43. #ifdef FURROVINEWIN
  44. QueryPerformanceCounter(&countstart);
  45. #elif FURROVINEAPPLE || FURROVINELINUX
  46. gettimeofday(&countstart, NULL);
  47. #endif
  48. }
  49.  
  50. void Stop (bool queryifstarted = true) {
  51. if ((queryifstarted && !stopped) || (!queryifstarted)) {
  52. #ifdef FURROVINEWIN
  53. QueryPerformanceCounter(&countend);
  54. QueryPerformanceFrequency( &frequency );
  55. #elif FURROVINEAPPLE || FURROVINELINUX
  56. gettimeofday(&countend, NULL);
  57. #endif
  58. }
  59. stopped = true;
  60. }
  61.  
  62. bool Running () {
  63. return !stopped;
  64. }
  65.  
  66. bool Stopped () {
  67. return stopped;
  68. }
  69.  
  70. int64 ElapsedTicks ( ) {
  71. #ifdef FURROVINEWIN
  72. if (!stopped)
  73. QueryPerformanceCounter(&countend);
  74.  
  75. return countend.QuadPart - countstart.QuadPart;
  76. #elif FURROVINEAPPLE || FURROVINELINUX
  77. if(!stopped)
  78. gettimeofday(&countend, NULL);
  79.  
  80. return countstart.tv_usec - countend.tv_usec;
  81. #endif
  82. }
  83.  
  84. double ElapsedSeconds () {
  85. #ifdef FURROVINEWIN
  86. if ( !stopped ) {
  87. QueryPerformanceCounter(&countend);
  88. QueryPerformanceFrequency( &frequency );
  89. }
  90.  
  91. return (countend.QuadPart - countstart.QuadPart ) / ( frequency.QuadPart * Mathema<double>::InversePow10[0] );
  92. #elif FURROVINEAPPLE || FURROVINELINUX
  93. if(!stopped)
  94. gettimeofday(&countend, NULL);
  95.  
  96. double microstarttime = (countstart.tv_sec * Mathema<double>::Pow10[7]) + countstart.tv_usec;
  97. double microendtime = (countend.tv_sec * Mathema<double>::Pow10[7]) + countend.tv_usec;
  98.  
  99. return microendtime - microstarttime;
  100. #endif
  101. }
  102.  
  103. double ElapsedMilliseconds() {
  104. #ifdef FURROVINEWIN
  105. if ( !stopped ) {
  106. QueryPerformanceCounter(&countend);
  107. QueryPerformanceFrequency( &frequency );
  108. }
  109.  
  110. return (countend.QuadPart - countstart.QuadPart ) / ( frequency.QuadPart * Mathema<double>::InversePow10[3] );
  111. #elif FURROVINEAPPLE || FURROVINELINUX
  112. if(!stopped)
  113. gettimeofday(&countend, NULL);
  114.  
  115. double microstarttime = (countstart.tv_sec * Mathema<double>::Pow10[7]) + countstart.tv_usec;
  116. double microendtime = (countend.tv_sec * Mathema<double>::Pow10[7]) + countend.tv_usec;
  117.  
  118. return microendtime - microstarttime;
  119. #endif
  120. }
  121.  
  122. double ElapsedMicroseconds() {
  123. #ifdef FURROVINEWIN
  124. if ( !stopped ) {
  125. QueryPerformanceCounter(&countend);
  126. QueryPerformanceFrequency( &frequency );
  127. }
  128.  
  129. return (countend.QuadPart - countstart.QuadPart ) / ( frequency.QuadPart * Mathema<double>::InversePow10[6] );
  130. #elif FURROVINEAPPLE || FURROVINELINUX
  131. if(!stopped)
  132. gettimeofday(&countend, NULL);
  133.  
  134. double microstarttime = (countstart.tv_sec * Mathema<double>::Pow10[7]) + countstart.tv_usec;
  135. double microendtime = (countend.tv_sec * Mathema<double>::Pow10[7]) + countend.tv_usec;
  136.  
  137. return microendtime - microstarttime;
  138. #endif
  139. }
  140.  
  141. TimeSpan ElapsedTime () {
  142. #ifdef FURROVINEWIN
  143. if ( !stopped ) {
  144. QueryPerformanceCounter(&countend);
  145. QueryPerformanceFrequency( &frequency );
  146. }
  147.  
  148. return TimeSpan( countend.QuadPart - countstart.QuadPart );
  149. #elif FURROVINEAPPLE || FURROVINELINUX
  150. if(!stopped)
  151. gettimeofday(&countend, NULL);
  152.  
  153. return TimeSpan( countstart.tv_usec - countend.tv_usec );
  154. #endif
  155. }
  156.  
  157. };
  158.  
  159. }
  160.  
  161. #endif /* FURROVINESTOPWATCH_H */
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty