00001 00002 00003 00015 00016 // Copyright (C) 1997-2010 by Pawel Pilarczyk. 00017 // 00018 // This file is part of the Homology Library. This library is free software; 00019 // you can redistribute it and/or modify it under the terms of the GNU 00020 // General Public License as published by the Free Software Foundation; 00021 // either version 2 of the License, or (at your option) any later version. 00022 // 00023 // This library is distributed in the hope that it will be useful, 00024 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00025 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00026 // GNU General Public License for more details. 00027 // 00028 // You should have received a copy of the GNU General Public License along 00029 // with this software; see the file "license.txt". If not, write to the 00030 // Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, 00031 // MA 02111-1307, USA. 00032 00033 // You may want to include "textfile.h" before this file. 00034 // Started on March 23, 2002. Last revision: February 9, 2003. 00035 00036 // NOTE: If you do not want to control the time measurement, but only want 00037 // to make your program display a "Time used" message at its termination, 00038 // then it is enough that you only link your program with "timeused.cpp" 00039 // without even including this header file in your code. 00040 00041 00042 #ifndef _CHOMP_SYSTEM_TIMEUSED_H_ 00043 #define _CHOMP_SYSTEM_TIMEUSED_H_ 00044 00045 #include "chomp/system/config.h" 00046 00047 #include <ctime> 00048 #include <iostream> 00049 00050 namespace chomp { 00051 namespace homology { 00052 00053 00054 // classes defined within this header file: 00055 class timeused; 00056 00057 00058 // -------------------------------------------------- 00059 // ------------------- TIMEUSED --------------------- 00060 // -------------------------------------------------- 00061 00067 class timeused 00068 { 00069 public: 00072 timeused (const char *msg = NULL); 00073 timeused (std::ostream &out, const char *msg = NULL); 00074 00076 ~timeused (); 00077 00080 timeused &operator = (std::ostream &out); 00081 00082 #ifdef OUTPUTSTREAM 00083 00084 00085 timeused &operator = (outputstream &out); 00086 #endif 00087 00090 timeused &operator = (int n); 00091 00093 timeused &operator = (const char *msg); 00094 00096 timeused &reset (); 00097 00099 operator double (); 00100 00103 void show (std::ostream &out, const char *message = NULL) const; 00104 00108 void show (const char *message = NULL) const; 00109 00111 friend std::ostream &operator << (std::ostream &out, 00112 const timeused &t); 00113 00114 protected: 00116 double cpu0; 00117 00119 std::time_t t0; 00120 00122 std::ostream *outstream1; 00123 00125 std::ostream *outstream2; 00126 00128 const char *message; 00129 00132 int display; 00133 00134 }; /* timeused */ 00135 00136 // -------------------------------------------------- 00137 00138 inline timeused::timeused (const char *msg) 00139 { 00140 reset (); 00141 outstream1 = NULL; 00142 outstream2 = NULL; 00143 message = msg; 00144 display = -1; 00145 return; 00146 } /* timeused::timeused */ 00147 00148 inline timeused::timeused (std::ostream &out, const char *msg) 00149 { 00150 reset (); 00151 outstream1 = &out; 00152 outstream2 = NULL; 00153 message = msg; 00154 display = -1; 00155 return; 00156 } /* timeused::timeused */ 00157 00158 inline void timeused::show (std::ostream &out, const char *msg) const 00159 { 00160 if (!msg) 00161 msg = message; 00162 if (!msg) 00163 msg = "Time used:"; 00164 00165 out << msg << ' ' << *this << '.' << std::endl; 00166 00167 return; 00168 } /* timeused::show */ 00169 00170 inline timeused::~timeused () 00171 { 00172 if (!display || (!outstream1 && !outstream2)) 00173 return; 00174 if ((display > 0) || (double (*this) > 1)) 00175 { 00176 if (outstream1) 00177 show (*outstream1); 00178 if (outstream2) 00179 show (*outstream2); 00180 } 00181 return; 00182 } /* timeused::~timeused */ 00183 00184 inline timeused &timeused::operator = (std::ostream &out) 00185 { 00186 outstream1 = &out; 00187 outstream2 = NULL; 00188 return *this; 00189 } /* timeused::operator = */ 00190 00191 #ifdef OUTPUTSTREAM 00192 inline timeused &timeused::operator = (outputstream &out) 00193 { 00194 if (out. show) 00195 outstream1 = &(out. out); 00196 else 00197 outstream1 = NULL; 00198 if (out. log) 00199 outstream2 = out. getlogstream (); 00200 else 00201 outstream2 = NULL; 00202 return *this; 00203 } /* operator = */ 00204 #endif 00205 00206 inline timeused &timeused::operator = (int n) 00207 { 00208 display = n; 00209 return *this; 00210 } /* timeused::operator = */ 00211 00212 inline timeused &timeused::operator = (const char *msg) 00213 { 00214 message = msg; 00215 return *this; 00216 } /* timeused::operator = */ 00217 00218 inline void timeused::show (const char *msg) const 00219 { 00220 if (outstream1) 00221 show (*outstream1, msg); 00222 if (outstream2) 00223 show (*outstream2, msg); 00224 return; 00225 } /* timeused::show */ 00226 00227 // -------------------------------------------------- 00228 00229 #ifndef TIMEUSED 00230 00231 00232 #define TIMEUSED 00233 #endif 00234 00239 extern timeused program_time; 00240 00241 00242 } // namespace homology 00243 } // namespace chomp 00244 00245 #endif // _CHOMP_SYSTEM_TIMEUSED_H_ 00246 00248