Effective implementation of algorithms (Master Thesis)
Effective and error-free implementation of algorithms
|
00001 #ifndef H_UTILS_TIMER_TIMER 00002 #define H_UTILS_TIMER_TIMER 00003 00007 #include <time.h> 00008 #include <stdexcept> 00009 00010 namespace utils { 00011 namespace timer { 00012 00029 class Timer { 00030 public: 00035 Timer() { 00036 reset(); 00037 } 00038 00043 void reset() { 00044 clock_t new_time = clock(); 00045 if (new_time == -1) { 00046 throw std::runtime_error("Error reading clock!"); 00047 } 00048 start_time = new_time; 00049 } 00050 00059 double elapsed_time_sec() { 00060 clock_t current = clock(); 00061 if (current == -1) { 00062 throw std::runtime_error("Error reading clock!"); 00063 } 00064 clock_t elapsed = current - start_time; 00065 return (double) elapsed / (double) CLOCKS_PER_SEC; 00066 } 00067 00068 private: 00070 clock_t start_time; 00071 }; 00072 00073 } // namespace timer 00074 } // namespace utils 00075 #endif