timer.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*! \file timer_linux.h
  2. * \brief Contains timer class that can be used by Linux systems.
  3. */
  4. #ifndef TIMER_LINUX_H
  5. #define TIMER_LINUX_H
  6. #include <sys/time.h>
  7. #include <iostream>
  8. #include <vector>
  9. class Timer
  10. {
  11. private:
  12. timeval timerStart;
  13. timeval timerEnd;
  14. std::vector<double> multi_time; // used for estimating multi backends.
  15. std::vector<double> time;
  16. bool record_multi;
  17. void addMultiMaxTime() // used to estimate when using multi-Backend
  18. {
  19. double max = 0.0f;
  20. //std::cout<<"AddMultiMaxTime call before"<<time.size()<<"\n";
  21. if(multi_time.empty())
  22. return;
  23. for(std::vector<double>::iterator it = multi_time.begin(); it != multi_time.end(); ++it)
  24. {
  25. if (max < *it)
  26. max = *it;
  27. }
  28. time.push_back( max );
  29. //std::cout<<"MAXXX:"<<max<<" ";
  30. multi_time.clear(); // clear it in both cases.
  31. //std::cout<<"AddMultiMaxTime call after reset"<<time.size()<<"\n";
  32. }
  33. void addMultiMinTime() // used to estimate when using multi-Backend
  34. {
  35. double min = 0.0f;
  36. if(multi_time.empty())
  37. return;
  38. for(std::vector<double>::iterator it = multi_time.begin(); it != multi_time.end(); ++it)
  39. {
  40. if (min > *it)
  41. min = *it;
  42. }
  43. time.push_back( min );
  44. multi_time.clear(); // clear it in both cases.
  45. }
  46. public:
  47. void start_record_multi()
  48. {
  49. record_multi=true;
  50. multi_time.clear(); // clear it in both cases.
  51. }
  52. void stop_record_multi()
  53. {
  54. addMultiMaxTime();
  55. record_multi=false;
  56. }
  57. Timer() {record_multi=false;}
  58. /*!
  59. * Starts the timimg.
  60. */
  61. void start()
  62. {
  63. gettimeofday(&timerStart, NULL);
  64. //std::cout<<"start_\n";
  65. }
  66. /*!
  67. * Stops the timimg and stores time in a vector.
  68. */
  69. void stop()
  70. {
  71. gettimeofday(&timerEnd, NULL);
  72. if(record_multi)
  73. multi_time.push_back( (timerEnd.tv_sec - timerStart.tv_sec + (timerEnd.tv_usec - timerStart.tv_usec) / 1000000.0) * 1000 );
  74. else
  75. time.push_back( (timerEnd.tv_sec - timerStart.tv_sec + (timerEnd.tv_usec - timerStart.tv_usec) / 1000000.0) * 1000 );
  76. //std::cout<<"stop_\n";
  77. }
  78. /*!
  79. * Clears all timings taken.
  80. */
  81. void reset()
  82. {
  83. if(!record_multi)
  84. {
  85. // std::cout<<"Time reset:\n";
  86. time.clear();
  87. }
  88. multi_time.clear(); // clear it in both cases.
  89. }
  90. /*!
  91. * \param run The run to get timing of.
  92. *
  93. * \return Time for a certain run.
  94. */
  95. double getTime(int run = 0)
  96. {
  97. return time.at(run);
  98. }
  99. /*!
  100. * \return Total time of all stored runs.
  101. */
  102. double getTotalTime()
  103. {
  104. double totalTime = 0.0f;
  105. for(std::vector<double>::iterator it = time.begin(); it != time.end(); ++it)
  106. {
  107. totalTime += *it;
  108. }
  109. return totalTime;
  110. }
  111. /*!
  112. * \return Average time of all stored runs.
  113. */
  114. double getAverageTime()
  115. {
  116. double totalTime = 0.0f;
  117. for(std::vector<double>::iterator it = time.begin(); it != time.end(); ++it)
  118. {
  119. totalTime += *it;
  120. }
  121. return (double)(totalTime/time.size());
  122. }
  123. double getMaxTime()
  124. {
  125. double max = 0.0f;
  126. for(std::vector<double>::iterator it = time.begin(); it != time.end(); ++it)
  127. {
  128. if (max < *it)
  129. max = *it;
  130. }
  131. return max;
  132. }
  133. double getMinTime()
  134. {
  135. double min = 0.0f;
  136. for(std::vector<double>::iterator it = time.begin(); it != time.end(); ++it)
  137. {
  138. if (min > *it)
  139. min = *it;
  140. }
  141. return min;
  142. }
  143. /*!
  144. * \return The resolution of the timer in micro seconds.
  145. */
  146. double getResolutionUs()
  147. {
  148. double result = 0.0f;
  149. timeval tStart;
  150. timeval tEnd;
  151. gettimeofday(&tStart, NULL);
  152. gettimeofday(&tEnd, NULL);
  153. int delay = 0;
  154. do
  155. {
  156. delay++;
  157. gettimeofday(&tStart, NULL);
  158. for(int i = 0; i < delay; ++i) ;
  159. gettimeofday(&tEnd, NULL);
  160. result = ((((double)tEnd.tv_sec)*1000000.0) + ((double)tEnd.tv_usec)) - ((((double)tStart.tv_sec)*1000000.0) + ((double)tStart.tv_usec));
  161. } while(result == 0);
  162. return result;
  163. }
  164. /*!
  165. * \return Number of runs stored in timer.
  166. */
  167. int getNumTimings()
  168. {
  169. return time.size();
  170. }
  171. };
  172. #endif