Timer.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. * NICE-Core - efficient algebra and computer vision methods
  3. * - libfbasics - library of some basic tools
  4. * See file License for license information.
  5. */
  6. #ifndef _FBASICS_TIMER_H_
  7. #define _FBASICS_TIMER_H_
  8. #ifdef LIMUN_AIBO_MODE
  9. #ifndef PLATFORM_APERIOS
  10. #define PLATFORM_APERIOS
  11. #endif
  12. //#include <MCOOP.h>
  13. #include <Shared/TimeET.h>
  14. #else
  15. #include <ctime>
  16. #include <sys/time.h>
  17. #endif
  18. #include <string>
  19. #include <core/basics/Log.h>
  20. namespace NICE {
  21. /**
  22. * A class to measure CPU time consumption. All times are measured in seconds.
  23. * There are currently two measurement techniques refered to as "process time"
  24. * and "absolute time". Process time only measures time while the process is
  25. * active. Absolute time is indepentent of scheduling, but offers much higher
  26. * precision and can measure longer times (see note).
  27. * \note On a typical GNU Linux system, process time measurement
  28. * can only handle a maximum of approx 71.5 minutes
  29. * between calls to start() and stop(). Otherwise overflows occur
  30. * and the results are incorrect.
  31. */
  32. class Timer {
  33. public:
  34. /**
  35. * @name Constructors and destructor
  36. * \{
  37. */
  38. /**
  39. * Create a unnamed timer which will not print results to timing log.
  40. */
  41. Timer();
  42. /**
  43. * Create a named timer which will (optionally) print results to timing log
  44. * Log::timing() when destroyed.
  45. */
  46. Timer ( const std::string _name, bool _printToLogWhenDestroyed = true );
  47. ~Timer();
  48. /**
  49. * \}
  50. * @name Measuring control.
  51. * \{
  52. */
  53. /**
  54. * Start time measurement.
  55. */
  56. void start();
  57. /**
  58. * Stop time measurement.
  59. */
  60. void stop();
  61. /**
  62. * Reset measurements.
  63. */
  64. void reset();
  65. /**
  66. * The number of calls to stop() since construction or last call of reset().
  67. */
  68. inline int getCounter() const {
  69. return counter;
  70. }
  71. /**
  72. * \}
  73. * @name Get measurement results - low resolution process time.
  74. * \{
  75. */
  76. /**
  77. * Value of the previous measurement in process time.
  78. * @return previous measurement
  79. */
  80. inline double getLast() const {
  81. return last;
  82. }
  83. /**
  84. * Sum of all measurement (since last \c reset()) in process time.
  85. * @return Sum of all measurements
  86. */
  87. inline double getSum() const {
  88. return sum;
  89. }
  90. /**
  91. * Mean of the all measurement (since last \c reset()) in process time.
  92. * @return Mean of all measurements
  93. */
  94. inline double getMean() const {
  95. return sum / ( double ) counter;
  96. }
  97. /**
  98. * \}
  99. * @name Get measurement results - high resolution absolute time.
  100. * \{
  101. */
  102. /**
  103. * Value of the previous measurement in absolute time.
  104. * @return previous measurement
  105. */
  106. inline double getLastAbsolute() const {
  107. return lastAbsolute;
  108. }
  109. /**
  110. * Sum of all measurement (since last \c reset()) in absolute time.
  111. * @return Sum of all measurements
  112. */
  113. inline double getSumAbsolute() const {
  114. return sumAbsolute;
  115. }
  116. /**
  117. * Mean of the all measurement (since last \c reset()) in absolute time.
  118. * @return Mean of all measurements
  119. */
  120. inline double getMeanAbsolute() const {
  121. return sumAbsolute / ( double ) counter;
  122. }
  123. /**
  124. * \}
  125. * @name Estimation of finishing time of some task.
  126. * \{
  127. */
  128. /**
  129. * double value of start time in seconds since Epoch
  130. * (00:00:00 UTC, January 1, 1970)
  131. * @return double value of start time in sec
  132. */
  133. double getStartTime() const;
  134. /**
  135. * double value of estimated end time by multiplying the elapsed absolute
  136. * sum with the factor \c fac.
  137. * @param fac factor to multiply the elapsed absolute time sum
  138. * @note use in a loop of i=0...max : fac = max/(i+1.)
  139. * @return estimated end time in seconds since Epoch
  140. * (00:00:00 UTC, January 1, 1970)
  141. */
  142. double getEstimatedEndTime ( double fac ) const;
  143. /**
  144. * \}
  145. * @name Time functions, NOT related to measuring periods of time.
  146. * \{
  147. */
  148. /**
  149. * Current time in seconds since Epoch (00:00:00 UTC, January 1, 1970).
  150. * Precision is up to one microsecond.
  151. */
  152. static double getNow();
  153. /**
  154. * Get current date and local time as a string of the following format:
  155. * yyyy-mm-dd-hh-mm-ss.
  156. */
  157. static std::string getNowString();
  158. /**
  159. * Get current microseconds - a pretty useless value, but nice for srand().
  160. */
  161. static long int getMicroseconds();
  162. /**
  163. * convert timeval format to double value of time in seconds since Epoch
  164. * (00:00:00 UTC, January 1, 1970)
  165. * @return double value of time in seconds
  166. */
  167. static inline double convertTime ( const struct timeval &time ) {
  168. return double ( time.tv_sec ) + double ( time.tv_usec ) * 1e-6;
  169. }
  170. /**
  171. * Date string from time since Epoch (00:00:00 UTC, January 1, 1970),
  172. * measured in seconds
  173. * @ sec seconds since 00:00:00 UTC, January 1, 1970
  174. * @return date string
  175. */
  176. static std::string timeToString ( time_t sec );
  177. /**
  178. * see \c timeToString(time_t)
  179. */
  180. static std::string timeToString ( double sec );
  181. /**
  182. * \}
  183. */
  184. private:
  185. //! Start time of current measurement
  186. clock_t startClock;
  187. //! Start time of current measurement
  188. double startTimeAbsolute;
  189. //! Previous measurement
  190. double last;
  191. //! Sum of all measurement (since last \c reset())
  192. double sum;
  193. //! Previous measurement
  194. double lastAbsolute;
  195. //! Sum of all measurement (since last \c reset())
  196. double sumAbsolute;
  197. //! Number of measurements (since last \c reset())
  198. int counter;
  199. //! Print results in destructor?
  200. bool printToLogWhenDestroyed;
  201. //! Name of the timer.
  202. std::string name;
  203. //! internal
  204. double getCurrentAbsoluteTime() const;
  205. };
  206. } // namespace
  207. #endif // _FBASICS_TIMER_H_