The Original CHomP Software
timeused.h
Go to the documentation of this file.
1
3
15
16// Copyright (C) 1997-2020 by Pawel Pilarczyk.
17//
18// This file is part of my research software package. This is free software:
19// you can redistribute it and/or modify it under the terms of the GNU
20// General Public License as published by the Free Software Foundation,
21// either version 3 of the License, or (at your option) any later version.
22//
23// This software is distributed in the hope that it will be useful,
24// but WITHOUT ANY WARRANTY; without even the implied warranty of
25// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26// GNU General Public License for more details.
27//
28// You should have received a copy of the GNU General Public License
29// along with this software; see the file "license.txt". If not,
30// please, see <https://www.gnu.org/licenses/>.
31
32// You may want to include "textfile.h" before this file.
33// Started on March 23, 2002. Last revision: February 9, 2003.
34
35// NOTE: If you do not want to control the time measurement, but only want
36// to make your program display a "Time used" message at its termination,
37// then it is enough that you only link your program with "timeused.cpp"
38// without even including this header file in your code.
39
40
41#ifndef _CHOMP_SYSTEM_TIMEUSED_H_
42#define _CHOMP_SYSTEM_TIMEUSED_H_
43
44#include "chomp/system/config.h"
45
46#include <ctime>
47#include <iostream>
48
49namespace chomp {
50namespace homology {
51
52
53// classes defined within this header file:
54class timeused;
55
56
57// --------------------------------------------------
58// ------------------- TIMEUSED ---------------------
59// --------------------------------------------------
60
67{
68public:
71 timeused (const char *msg = NULL);
72 timeused (std::ostream &out, const char *msg = NULL);
73
75 ~timeused ();
76
79 timeused &operator = (std::ostream &out);
80
81 #ifdef OUTPUTSTREAM
85 #endif
86
89 timeused &operator = (int n);
90
92 timeused &operator = (const char *msg);
93
96
98 operator double ();
99
102 void show (std::ostream &out, const char *message = NULL) const;
103
107 void show (const char *message = NULL) const;
108
110 friend std::ostream &operator << (std::ostream &out,
111 const timeused &t);
112
113protected:
115 double cpu0;
116
118 std::time_t t0;
119
121 std::ostream *outstream1;
122
124 std::ostream *outstream2;
125
127 const char *message;
128
132
133}; /* timeused */
134
135// --------------------------------------------------
136
137inline timeused::timeused (const char *msg)
138{
139 reset ();
140 outstream1 = NULL;
141 outstream2 = NULL;
142 message = msg;
143 display = -1;
144 return;
145} /* timeused::timeused */
146
147inline timeused::timeused (std::ostream &out, const char *msg)
148{
149 reset ();
150 outstream1 = &out;
151 outstream2 = NULL;
152 message = msg;
153 display = -1;
154 return;
155} /* timeused::timeused */
156
157inline void timeused::show (std::ostream &out, const char *msg) const
158{
159 if (!msg)
160 msg = message;
161 if (!msg)
162 msg = "Time used:";
163
164 out << msg << ' ' << *this << '.' << std::endl;
165
166 return;
167} /* timeused::show */
168
170{
171 if (!display || (!outstream1 && !outstream2))
172 return;
173 if ((display > 0) || (double (*this) > 1))
174 {
175 if (outstream1)
176 show (*outstream1);
177 if (outstream2)
178 show (*outstream2);
179 }
180 return;
181} /* timeused::~timeused */
182
183inline timeused &timeused::operator = (std::ostream &out)
184{
185 outstream1 = &out;
186 outstream2 = NULL;
187 return *this;
188} /* timeused::operator = */
189
190#ifdef OUTPUTSTREAM
192{
193 if (out. show)
194 outstream1 = &(out. out);
195 else
196 outstream1 = NULL;
197 if (out. log)
198 outstream2 = out. getlogstream ();
199 else
200 outstream2 = NULL;
201 return *this;
202} /* operator = */
203#endif
204
206{
207 display = n;
208 return *this;
209} /* timeused::operator = */
210
211inline timeused &timeused::operator = (const char *msg)
212{
213 message = msg;
214 return *this;
215} /* timeused::operator = */
216
217inline void timeused::show (const char *msg) const
218{
219 if (outstream1)
220 show (*outstream1, msg);
221 if (outstream2)
222 show (*outstream2, msg);
223 return;
224} /* timeused::show */
225
226// --------------------------------------------------
227
228#ifndef TIMEUSED
231#define TIMEUSED
232#endif
233
239
240
241} // namespace homology
242} // namespace chomp
243
244#endif // _CHOMP_SYSTEM_TIMEUSED_H_
245
247
This class defines an output stream for replacing the standard 'cout'.
Definition: textfile.h:64
A class that stores the time at which it was initialized and then returns or displays the time used s...
Definition: timeused.h:67
friend std::ostream & operator<<(std::ostream &out, const timeused &t)
Shows the time elapsed up to this point.
timeused & reset()
Reset the timer to the current moment.
int display
Should the destructor display the time? Note: -1 makes the destructor display times only > 1 sec.
Definition: timeused.h:131
timeused(const char *msg=NULL)
The default constructor.
Definition: timeused.h:137
timeused & operator=(std::ostream &out)
Defines an output stream for displaying program's running time at program's termination.
Definition: timeused.h:183
std::ostream * outstream2
Output stream 2 (0 for no output).
Definition: timeused.h:124
void show(std::ostream &out, const char *message=NULL) const
Shows the time used from the beginning up to the current point.
Definition: timeused.h:157
double cpu0
CPU usage start time (in seconds).
Definition: timeused.h:115
std::time_t t0
Start time (in seconds).
Definition: timeused.h:118
~timeused()
The destructor.
Definition: timeused.h:169
std::ostream * outstream1
Output stream 1 (0 for no output).
Definition: timeused.h:121
const char * message
A message to display instead of "Used time" (if not 0).
Definition: timeused.h:127
This file contains some precompiler definitions which indicate the operating system and/or compiler u...
timeused program_time
The external variable which measures the time used by the program from its start.
This namespace contains the entire CHomP library interface.
Definition: bitmaps.h:51