00001 00002 // 00003 // Copyright (C) 2000 00004 // Ralf Westram 00005 // (Coded@ReallySoft.de) 00006 // 00007 // Permission to use, copy, modify, distribute and sell this software 00008 // and its documentation for any purpose is hereby granted without fee, 00009 // provided that the above copyright notice appear in all copies and 00010 // that both that copyright notice and this permission notice appear 00011 // in supporting documentation. Ralf Westram makes no 00012 // representations about the suitability of this software for any 00013 // purpose. It is provided "as is" without express or implied warranty. 00014 // 00015 // This code is part of my library. 00016 // You may find a more recent version at http://www.reallysoft.de/ 00017 // 00019 00020 #ifndef TIMER_H 00021 #define TIMER_H 00022 00023 #include <ctime> 00024 #include <string> 00025 #include <iostream> 00026 00027 #ifdef NDEBUG 00028 // define SHOW_TIMERS before including header to show Timers in non-debug code 00029 #else 00030 # ifndef DONT_SHOW_TIMERS 00031 // define DONT_SHOW_TIMERS before including header to hide Timers in debug code 00032 # define SHOW_TIMERS 00033 # endif 00034 #endif 00035 00036 namespace rs { 00037 00038 // -------------------------------------------------------------------------------- 00039 // class Timer 00040 // -------------------------------------------------------------------------------- 00041 00044 class Timer { 00045 private: 00046 time_t start; 00047 std::string description; 00048 std::ostream& out; 00049 size_t counter; 00050 std::string count_description; 00051 static int timer_counter; 00052 00053 size_t uptime() { 00054 time_t now; 00055 time(&now); 00056 return now-start; 00057 } 00058 00059 void indent() { 00060 for (int i=0; i<timer_counter; i++) { 00061 out << '*'; 00062 } 00063 } 00064 00065 public: 00074 Timer(std::ostream& aOut, const std::string& desc) 00075 : description(desc), 00076 out(aOut), 00077 counter(0), 00078 count_description("none") 00079 { 00080 time(&start); 00081 timer_counter++; 00082 #if !defined(SHOW_TIMERS) 00083 indent(); 00084 out << "Started " << description << "..\n"; 00085 #endif // SHOW_TIMERS 00086 } 00087 00088 void show() { 00089 #if !defined(SHOW_TIMERS) 00090 size_t seconds = uptime(); 00091 if (seconds==0) seconds = 1; 00092 00093 indent(); 00094 out << "Stopped " << description << " after " << seconds << " seconds."; 00095 if (counter) { 00096 out << ' ' << count_description << '=' << counter << "; per sec=" << double(counter)/seconds; 00097 } 00098 out << "\n"; 00099 #endif // SHOW_TIMERS 00100 } 00101 00102 virtual ~Timer() { 00103 show(); 00104 timer_counter--; 00105 } 00106 00116 void set_counter(size_t aCount, const std::string& count_desc) { 00117 counter = aCount; 00118 count_description = count_desc; 00119 } 00120 }; 00121 00122 }; 00123 00124 #else 00125 #error Timer.h included twice 00126 #endif // TIMER_H
Contact me in case of errors or questions. This documentation is powered by ![]() |
(C) 2000-2002 ![]() |