Changeset 8e24ef for src/Helpers/Chronos.cpp
- Timestamp:
- Apr 2, 2011, 12:20:10 AM (15 years ago)
- Children:
- bbd746
- Parents:
- 93abe8
- git-author:
- Frederik Heber <heber@…> (03/15/11 09:53:34)
- git-committer:
- Frederik Heber <heber@…> (04/02/11 00:20:10)
- File:
-
- 1 edited
-
src/Helpers/Chronos.cpp (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/Helpers/Chronos.cpp
r93abe8 r8e24ef 22 22 #include <iostream> 23 23 24 #ifdef HAVE_SYS_TIMES_H 25 # include <sys/times.h> 26 #else 27 # include <time.h> 28 #endif 29 24 30 #include "Chronos.hpp" 25 31 … … 32 38 {} 33 39 34 intChronos::getTime(const std::string _name) const40 double Chronos::getTime(const std::string _name) const 35 41 { 36 42 // only those functions have a time that have run already 37 if ( IsTimeRunning.count(_name)) {43 if (TimeRunning.count(_name)) { 38 44 // return -1 if function is currently running 39 if ( !IsTimeRunning.count(_name))45 if (TimeRunning.count(_name) != 0.) 40 46 return AccountedTime.at(_name); 41 47 else 42 return -1 ;48 return -1.; 43 49 } 44 return 0 ;50 return 0.; 45 51 } 46 52 47 53 void Chronos::resetTime(const std::string _name) 48 54 { 49 if ( IsTimeRunning.count(_name)) {50 AccountedTime[_name] = 0 ;55 if (TimeRunning.count(_name)) { 56 AccountedTime[_name] = 0.; 51 57 } 52 58 } … … 55 61 { 56 62 // start time keeping 57 IsTimeRunning[_name] = true; 63 TimeRunning[_name] = getCurrentTime(); 64 } 65 66 double Chronos::getCurrentTime() const 67 { 68 #ifdef HAVE_SYS_TIMES_H 69 struct tms *buffer = new tms; 70 double currenttime; 71 if (times(buffer) != (clock_t)(-1)) 72 currenttime = ((double)buffer->tms_utime/(double)CLOCKS_PER_SEC); 73 else 74 currenttime = 0.; 75 delete buffer; 76 #else 77 const double currenttime = (clock()/(double)CLOCKS_PER_SEC); 78 #endif 79 //std::cout << "Current time is " << currenttime << std::endl; 80 return currenttime; 58 81 } 59 82 60 83 void Chronos::endTiming(const std::string _name) 61 84 { 62 // finish time keeping if present 63 ASSERT(IsTimeRunning.count(_name), "Chronos::endTiming() - no timer under "+_name+" running."); 64 IsTimeRunning[_name] = false; 85 const double endtime = getCurrentTime(); 86 const double starttime = TimeRunning[_name]; 87 88 // if present 89 ASSERT(TimeRunning.count(_name), "Chronos::endTiming() - no timer under "+_name+" running."); 90 // finish time keeping 91 const double RunTime = ((double)endtime - starttime); 92 TimekeepingMap::iterator iter = AccountedTime.find(_name); 93 if (iter != AccountedTime.end()) 94 AccountedTime[_name] += RunTime; 95 else 96 AccountedTime[_name] = RunTime; 97 98 // and zero for next run 99 TimeRunning[_name] = 0.; 65 100 } 66 101 67 intChronos::SumUpTotalTime() const102 double Chronos::SumUpTotalTime() const 68 103 { 69 int sum = 0;104 double sum = 0.; 70 105 for (TimekeepingMap::const_iterator iter = AccountedTime.begin(); 71 106 iter != AccountedTime.end(); … … 78 113 size_t Chronos::SumUpTotalFunctions() const 79 114 { 80 return IsTimeRunning.size();115 return TimeRunning.size(); 81 116 } 82 117 83 118 std::ostream& operator<<(std::ostream &ost, const Chronos &_time) 84 119 { 85 int sum = _time.SumUpTotalTime(); 86 ost << "Total time passed: " << sum << std::endl; 120 ost << "List of functions present:" << std::endl; 121 for (Chronos::TimekeepingMap::const_iterator iter = _time.AccountedTime.begin(); 122 iter != _time.AccountedTime.end(); 123 ++iter) 124 ost << "\t" << iter->first << "\t" << iter->second << "s" << std::endl; 125 ost << "Total time passed: " << _time.SumUpTotalTime() << std::endl; 87 126 ost << "Total functions: " << _time.SumUpTotalFunctions() << std::endl; 88 89 127 return ost; 90 128 } 91 129 130 // construct the remainder of the singleton 131 CONSTRUCT_SINGLETON(Chronos) 92 132 93 CONSTRUCT_SINGLETON(Chronos) 133 // catch if someone wants to use Info objects in here 134 #ifdef INFO_HPP_ 135 BOOST_PP_ASSERT_MSG(1,\ 136 ERROR: This is a safety measure to generate a compiler warning\n \ 137 if you really try to use info.hpp in __FILE__.) 138 #endif 139
Note:
See TracChangeset
for help on using the changeset viewer.
