~ubuntu-branches/ubuntu/wily/openms/wily

« back to all changes in this revision

Viewing changes to include/OpenMS/SYSTEM/StopWatch.h

  • Committer: Package Import Robot
  • Author(s): Filippo Rusconi
  • Date: 2012-11-12 15:58:12 UTC
  • Revision ID: package-import@ubuntu.com-20121112155812-vr15wtg9b50cuesg
Tags: upstream-1.9.0
ImportĀ upstreamĀ versionĀ 1.9.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// -*- mode: C++; tab-width: 2; -*-
 
2
// vi: set ts=2:
 
3
//
 
4
// --------------------------------------------------------------------------
 
5
//                   OpenMS Mass Spectrometry Framework 
 
6
// --------------------------------------------------------------------------
 
7
//  Copyright (C) 2003-2011 -- Oliver Kohlbacher, Knut Reinert
 
8
//
 
9
//  This library is free software; you can redistribute it and/or
 
10
//  modify it under the terms of the GNU Lesser General Public
 
11
//  License as published by the Free Software Foundation; either
 
12
//  version 2.1 of the License, or (at your option) any later version.
 
13
//
 
14
//  This library is distributed in the hope that it will be useful,
 
15
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
17
//  Lesser General Public License for more details.
 
18
//
 
19
//  You should have received a copy of the GNU Lesser General Public
 
20
//  License along with this library; if not, write to the Free Software
 
21
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
22
//
 
23
// --------------------------------------------------------------------------
 
24
// $Maintainer: Timo Sachsenberg $
 
25
// $Authors: Marc Sturm $
 
26
// --------------------------------------------------------------------------
 
27
 
 
28
#ifndef OPENMS_SYSTEM_STOPWATCH_H
 
29
#define OPENMS_SYSTEM_STOPWATCH_H
 
30
 
 
31
#include <OpenMS/config.h>
 
32
 
 
33
#include <OpenMS/CONCEPT/Types.h>
 
34
 
 
35
#ifdef OPENMS_HAS_SYS_TIME_H
 
36
#include <sys/time.h>
 
37
#endif
 
38
 
 
39
#ifdef OPENMS_HAS_TIME_H
 
40
#include <ctime>
 
41
#endif
 
42
 
 
43
#include <iostream>
 
44
 
 
45
namespace OpenMS
 
46
{
 
47
        /**
 
48
                @brief StopWatch Class.
 
49
                
 
50
                This class is used to determine the current process time.
 
51
                
 
52
                @ingroup System
 
53
        */
 
54
        class OPENMS_DLLAPI StopWatch
 
55
        {
 
56
                public:
 
57
                
 
58
                /**     @name   Constructors and Destructors 
 
59
                */
 
60
                //@{
 
61
 
 
62
                /**     Default constructor.
 
63
                                Create a new stop watch. The stop watch is stopped.
 
64
                */
 
65
                StopWatch();                            
 
66
 
 
67
                /**     Copy constructor.
 
68
                                Create a new stop watch from an existing stop watch.
 
69
                */
 
70
                StopWatch(const StopWatch& stop_watch);                         
 
71
 
 
72
                /**     Destructor.
 
73
                                Destructs a stop watch object.
 
74
                */
 
75
                virtual ~StopWatch();
 
76
 
 
77
                //@}
 
78
                /**     Starting, Stopping and Resetting the stop watch 
 
79
                */
 
80
                //@{
 
81
 
 
82
                /**     Clear and stop the stop watch.
 
83
                                This sets the stop watch to zero and stops it when running.
 
84
                                @see    reset
 
85
                */
 
86
                void clear();                   
 
87
        
 
88
                /** Start the stop watch.
 
89
                                The stop watch is started. If the stop watch is already running, <b>false</b>
 
90
                                is returned.
 
91
                                @return bool <b>false</b> if the stop watch was already running, <b>true</b> otherwise
 
92
                */
 
93
                bool start();                   
 
94
 
 
95
                /** Stop the stop watch.
 
96
                                The stop watch is stopped. If the stop watch was not running, <b>false</b>
 
97
                                is returned.
 
98
                                @return bool <b>false</b> if the was not running, <b>true</b> otherwise
 
99
                */
 
100
                bool stop();
 
101
 
 
102
                /** Clear the stop watch without stopping.
 
103
                                The stop watch is cleared, but not stopped (if running).
 
104
                                @see clear
 
105
                */
 
106
                void reset();
 
107
 
 
108
                //@}
 
109
                        
 
110
                /**     @name Readout of the StopWatch 
 
111
                */
 
112
                //@{
 
113
 
 
114
                /**     Get clock time.
 
115
                                Return the accumulated clock (real) time in seconds.
 
116
                */
 
117
                DoubleReal getClockTime() const;
 
118
 
 
119
                /**     Get user time.
 
120
                                Return the accumulated user time in seconds.
 
121
                */
 
122
                DoubleReal getUserTime() const;         
 
123
 
 
124
                /**     Get user time.
 
125
                                Return the accumulated system time in seconds.
 
126
                */
 
127
                DoubleReal getSystemTime() const;
 
128
 
 
129
                /**     Get CPU time.
 
130
                                Return the accumulated CPU time in seconds.
 
131
                                CPU time is the sum of user time and system time.
 
132
                */
 
133
                inline DoubleReal getCPUTime() const 
 
134
                { 
 
135
                        return (getUserTime() + getSystemTime());
 
136
                }
 
137
 
 
138
 
 
139
                //@}
 
140
 
 
141
                /**     @name   Assignment
 
142
                */
 
143
                //@{
 
144
 
 
145
                /**     Assignment operator.
 
146
                                Assigns a stop watch from another. The two stop watch will then run 
 
147
                                synchronously.
 
148
                                @return StopWatch <tt>*this</tt>
 
149
                */
 
150
                StopWatch& operator = (const StopWatch& stop_watch);
 
151
 
 
152
                //@}
 
153
 
 
154
                /**     @name   Predicates 
 
155
                */
 
156
                //@{
 
157
 
 
158
                /**     Return true if the stop watch is running.
 
159
                                @return bool <b>true</b> if the stop watch is running, <b>false</b> otherwise
 
160
                */
 
161
                bool isRunning() const 
 
162
                {
 
163
                        return is_running_;
 
164
                }
 
165
 
 
166
                /**     Equality operator.
 
167
                                Return <b>true</b> if two stop watchs are equal, i.e. they contain exactly 
 
168
                                the same time intervals for clock, user and system time and have the
 
169
                                same running status.
 
170
                                @param stop_watch the stop watch to compare with
 
171
                                @return bool <b>true</b> on equality, <b>false</b> otherwise
 
172
                */
 
173
                bool operator == (const StopWatch& stop_watch) const;
 
174
 
 
175
                /**     Inequality operator.
 
176
                                Return <b>false</b> if two stop watchs differ in any way, i.e. they differ
 
177
                                in either the clock, user, or system time or have a different 
 
178
                                running status.
 
179
                                @param stop_watch the stop watch to compare with
 
180
                                @return bool <b>true</b> on inequality, <b>false</b> otherwise
 
181
                */      
 
182
                inline bool operator != (const StopWatch& stop_watch) const 
 
183
                {
 
184
                        return !(*this == stop_watch);
 
185
                }
 
186
 
 
187
 
 
188
                /**     Lesser than operator.
 
189
                                Return true, if the stop watch is in all timings lesser than the
 
190
                                stop watch to be compared with (clock, user and system time).
 
191
                                @param stop_watch the stop watch to compare with
 
192
                                @return bool <b>true</b> if all times are lesser
 
193
                */
 
194
                inline bool operator < (const StopWatch& stop_watch) const 
 
195
                {
 
196
                        return (getCPUTime() < stop_watch.getCPUTime());
 
197
                }
 
198
 
 
199
 
 
200
                /**     Lesser or equal operator.
 
201
                                Return true, if the stop watch is in all timings lesser or equal than the
 
202
                                stop watch to be compared with (clock, user and system time).
 
203
                                @param stop_watch the stop watch to compare with
 
204
                                @return bool <b>true</b> if all times are lesser or equal
 
205
                */
 
206
                inline bool operator <= (const StopWatch& stop_watch) const 
 
207
                {
 
208
                        return !(stop_watch < *this);
 
209
                }
 
210
 
 
211
 
 
212
                /**     Greater or equal operator.
 
213
                                Return true, if the stop watch is in all timings greater or equal than the
 
214
                                stop watch to be compared with (clock, user and system time).
 
215
                                @param stop_watch the stop watch to compare with
 
216
                                @return bool <b>true</b> if all times are greater or equal
 
217
                */
 
218
                inline bool operator >= (const StopWatch& stop_watch) const 
 
219
                {
 
220
                        return !(*this < stop_watch);
 
221
                }
 
222
 
 
223
 
 
224
                /**     Greater operator.
 
225
                                Return true, if the stop watch is in all timings greater than the
 
226
                                stop watch to be compared with (clock, user and system time).
 
227
                                @param stop_watch the stop watch to compare with
 
228
                                @return bool <b>true</b> if all times are greater 
 
229
                */
 
230
                inline bool operator > (const StopWatch& stop_watch) const 
 
231
                {
 
232
                        return (stop_watch < *this);
 
233
                }
 
234
 
 
235
 
 
236
                //@}
 
237
 
 
238
 
 
239
                private:
 
240
 
 
241
                static PointerSizeInt cpu_speed_;
 
242
 
 
243
                #ifdef OPENMS_WINDOWSPLATFORM
 
244
                        static PointerSizeInt clock_speed_;
 
245
                #endif
 
246
 
 
247
                // state of stop watch, either true(on) or false(off) 
 
248
                bool is_running_;
 
249
 
 
250
                // clock seconds value when the stop watch was last started 
 
251
                PointerSizeInt last_secs_;      
 
252
 
 
253
                // clock useconds value when the stop watch was last started 
 
254
                PointerSizeInt last_usecs_;             
 
255
 
 
256
                // user time when the stop watch was last started 
 
257
                clock_t last_user_time_;   
 
258
 
 
259
                // system time when the stop watch was last started 
 
260
                clock_t last_system_time_; 
 
261
                 
 
262
                // current accumulated clock seconds 
 
263
                PointerSizeInt current_secs_;           
 
264
 
 
265
                // current accumulated clock useconds 
 
266
                PointerSizeInt current_usecs_;          
 
267
                
 
268
                // current accumulated user time 
 
269
                clock_t current_user_time_;             
 
270
 
 
271
                // current accumulated user time 
 
272
                clock_t current_system_time_;
 
273
        };
 
274
 
 
275
}
 
276
 
 
277
#endif // OPENMS_SYSTEM_STOPWATCH_H