Timer.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. #include "core/basics/Timer.h"
  7. #include <core/basics/Exception.h>
  8. #include <limits>
  9. #ifdef WIN32
  10. #ifdef NICE_BOOST_FOUND
  11. #include "boost/date_time/posix_time/posix_time.hpp"
  12. #include "boost/date_time/gregorian/gregorian_types.hpp"
  13. #endif
  14. #endif
  15. namespace NICE {
  16. Timer::Timer() : printToLogWhenDestroyed ( false ), name ( "" ) {
  17. reset();
  18. }
  19. Timer::Timer ( const std::string _name, bool _printToLogWhenDestroyed )
  20. : printToLogWhenDestroyed ( _printToLogWhenDestroyed ), name ( _name ) {
  21. reset();
  22. }
  23. Timer::~Timer() {
  24. if ( printToLogWhenDestroyed ) {
  25. Log::timing() << std::fixed << "Timer " << name << ":"
  26. << " process_time=" << getSum()
  27. << ", mean=" << getMean()
  28. << "; absolute_time=" << getSumAbsolute()
  29. << ", mean=" << getMeanAbsolute() << std::endl;
  30. }
  31. }
  32. double Timer::getCurrentAbsoluteTime() const {
  33. #ifdef WIN32
  34. #ifdef NICE_BOOST_FOUND
  35. return getNow();
  36. #else
  37. return -1.0;
  38. #pragma message NICE_WARNING("Timer::getCurrentAbsoluteTime() : not yet ported to WIN32 plattform, returning -1")
  39. #endif
  40. #else
  41. #ifdef LIMUN_AIBO_MODE
  42. struct SystemTime t;
  43. GetSystemTime ( &t );
  44. return double ( t.seconds ) + double ( t.useconds ) * 1e-6;
  45. #else
  46. struct timezone tz;
  47. struct timeval tv;
  48. gettimeofday ( &tv, &tz );
  49. return convertTime ( tv );
  50. #endif
  51. #endif
  52. }
  53. void Timer::start() {
  54. startClock = clock();
  55. startTimeAbsolute = getCurrentAbsoluteTime();
  56. }
  57. void Timer::stop() {
  58. const clock_t endClock = clock();
  59. double passedClock = double ( endClock ) - double ( startClock );
  60. if ( endClock < startClock ) {
  61. passedClock += double ( std::numeric_limits<clock_t>::max() );
  62. }
  63. last = passedClock / double ( CLOCKS_PER_SEC );
  64. double endTimeAbsolute = getCurrentAbsoluteTime();
  65. lastAbsolute = endTimeAbsolute - startTimeAbsolute;
  66. counter++;
  67. sum += last;
  68. sumAbsolute += lastAbsolute;
  69. }
  70. double Timer::getStartTime() const
  71. {
  72. return startTimeAbsolute;
  73. }
  74. double Timer::getNow()
  75. {
  76. #ifdef WIN32
  77. #ifdef NICE_BOOST_FOUND
  78. boost::posix_time::ptime time_t_epoch( boost::gregorian::date(1970,1,1));
  79. boost::posix_time::ptime now = boost::posix_time::second_clock::local_time();
  80. boost::posix_time::time_duration diff = now - time_t_epoch;
  81. return diff.total_seconds();
  82. #else
  83. return -1.0;
  84. #pragma message NICE_WARNING("Timer::getNow() : not yet ported to WIN32 plattform, returning -1")
  85. #endif
  86. #else
  87. #ifdef LIMUN_AIBO_MODE
  88. fthrow ( Exception, "Not supported on AIBO." );
  89. #else
  90. struct timeval actTime;
  91. struct timezone tz;
  92. gettimeofday ( &actTime, &tz );
  93. return Timer::convertTime ( actTime );
  94. #endif
  95. #endif
  96. }
  97. std::string Timer::getNowString() {
  98. time_t nowTime = time ( 0 );
  99. tm* nowTm = localtime ( &nowTime );
  100. std::stringstream s;
  101. s << ( 1900 + nowTm->tm_year ) << "-" << ( nowTm->tm_mon + 1 )
  102. << "-" << nowTm->tm_mday << "-" << nowTm->tm_hour
  103. << "-" << nowTm->tm_min << "-" << nowTm->tm_sec;
  104. return s.str();
  105. }
  106. long int Timer::getMicroseconds() {
  107. #ifdef WIN32
  108. #ifdef NICE_BOOST_FOUND
  109. return getNow();
  110. #else
  111. return -1.0;
  112. #pragma message NICE_WARNING("Timer::getMicroseconds() : not yet ported to WIN32 plattform, returning -1")
  113. #endif
  114. #else
  115. #ifdef LIMUN_AIBO_MODE
  116. fthrow ( Exception, "Not supported on AIBO." );
  117. #else
  118. struct timeval actTime;
  119. struct timezone tz;
  120. gettimeofday ( &actTime, &tz );
  121. return actTime.tv_usec;
  122. #endif
  123. #endif
  124. }
  125. double Timer::getEstimatedEndTime ( double fac ) const
  126. {
  127. double actTime = getCurrentAbsoluteTime();
  128. double expected = ( actTime - startTimeAbsolute
  129. + sumAbsolute - lastAbsolute ) * ( fac - 1.0 );
  130. return actTime + expected;
  131. }
  132. std::string Timer::timeToString ( double sec )
  133. {
  134. return Timer::timeToString ( static_cast<time_t> ( sec ) );
  135. }
  136. std::string Timer::timeToString ( time_t sec )
  137. {
  138. std::string acttime = ctime ( &sec );
  139. acttime = acttime.substr ( 0, acttime.size() - 1 );
  140. return acttime;
  141. }
  142. void Timer::reset() {
  143. counter = 0;
  144. last = 0.0;
  145. sum = 0.0;
  146. lastAbsolute = 0.0;
  147. sumAbsolute = 0.0;
  148. }
  149. double Timer::convertTime ( const struct timeval &time ) {
  150. #ifndef WIN32
  151. return double ( time.tv_sec ) + double ( time.tv_usec ) * 1e-6;
  152. #else
  153. return -1.0;
  154. #pragma message NICE_WARNING("Timer::convertTime() : not yet ported to WIN32 plattform, returning -1")
  155. #endif
  156. }
  157. } // namespace