Timer.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. namespace NICE {
  10. Timer::Timer() : printToLogWhenDestroyed ( false ), name ( "" ) {
  11. reset();
  12. }
  13. Timer::Timer ( const std::string _name, bool _printToLogWhenDestroyed )
  14. : printToLogWhenDestroyed ( _printToLogWhenDestroyed ), name ( _name ) {
  15. reset();
  16. }
  17. Timer::~Timer() {
  18. if ( printToLogWhenDestroyed ) {
  19. Log::timing() << std::fixed << "Timer " << name << ":"
  20. << " process_time=" << getSum()
  21. << ", mean=" << getMean()
  22. << "; absolute_time=" << getSumAbsolute()
  23. << ", mean=" << getMeanAbsolute() << std::endl;
  24. }
  25. }
  26. double Timer::getCurrentAbsoluteTime() const {
  27. #ifdef LIMUN_AIBO_MODE
  28. struct SystemTime t;
  29. GetSystemTime ( &t );
  30. return double ( t.seconds ) + double ( t.useconds ) * 1e-6;
  31. #else
  32. struct timezone tz;
  33. struct timeval tv;
  34. gettimeofday ( &tv, &tz );
  35. return convertTime ( tv );
  36. #endif
  37. }
  38. void Timer::start() {
  39. startClock = clock();
  40. startTimeAbsolute = getCurrentAbsoluteTime();
  41. }
  42. void Timer::stop() {
  43. const clock_t endClock = clock();
  44. double passedClock = double ( endClock ) - double ( startClock );
  45. if ( endClock < startClock ) {
  46. passedClock += double ( std::numeric_limits<clock_t>::max() );
  47. }
  48. last = passedClock / double ( CLOCKS_PER_SEC );
  49. double endTimeAbsolute = getCurrentAbsoluteTime();
  50. lastAbsolute = endTimeAbsolute - startTimeAbsolute;
  51. counter++;
  52. sum += last;
  53. sumAbsolute += lastAbsolute;
  54. }
  55. double Timer::getStartTime() const
  56. {
  57. return startTimeAbsolute;
  58. }
  59. double Timer::getNow()
  60. {
  61. #ifdef LIMUN_AIBO_MODE
  62. fthrow ( Exception, "Not supported on AIBO." );
  63. #else
  64. struct timeval actTime;
  65. struct timezone tz;
  66. gettimeofday ( &actTime, &tz );
  67. return Timer::convertTime ( actTime );
  68. #endif
  69. }
  70. std::string Timer::getNowString() {
  71. time_t nowTime = time ( 0 );
  72. tm* nowTm = localtime ( &nowTime );
  73. std::stringstream s;
  74. s << ( 1900 + nowTm->tm_year ) << "-" << ( nowTm->tm_mon + 1 )
  75. << "-" << nowTm->tm_mday << "-" << nowTm->tm_hour
  76. << "-" << nowTm->tm_min << "-" << nowTm->tm_sec;
  77. return s.str();
  78. }
  79. long int Timer::getMicroseconds() {
  80. #ifdef LIMUN_AIBO_MODE
  81. fthrow ( Exception, "Not supported on AIBO." );
  82. #else
  83. struct timeval actTime;
  84. struct timezone tz;
  85. gettimeofday ( &actTime, &tz );
  86. return actTime.tv_usec;
  87. #endif
  88. }
  89. double Timer::getEstimatedEndTime ( double fac ) const
  90. {
  91. double actTime = getCurrentAbsoluteTime();
  92. double expected = ( actTime - startTimeAbsolute
  93. + sumAbsolute - lastAbsolute ) * ( fac - 1.0 );
  94. return actTime + expected;
  95. }
  96. std::string Timer::timeToString ( double sec )
  97. {
  98. return Timer::timeToString ( static_cast<time_t> ( sec ) );
  99. }
  100. std::string Timer::timeToString ( time_t sec )
  101. {
  102. std::string acttime = ctime ( &sec );
  103. acttime = acttime.substr ( 0, acttime.size() - 1 );
  104. return acttime;
  105. }
  106. void Timer::reset() {
  107. counter = 0;
  108. last = 0.0;
  109. sum = 0.0;
  110. lastAbsolute = 0.0;
  111. sumAbsolute = 0.0;
  112. }
  113. } // namespace