Timer.h 5.9 KB

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