OpenMS
StopWatch.h
Go to the documentation of this file.
1 // --------------------------------------------------------------------------
2 // OpenMS -- Open-Source Mass Spectrometry
3 // --------------------------------------------------------------------------
4 // Copyright The OpenMS Team -- Eberhard Karls University Tuebingen,
5 // ETH Zurich, and Freie Universitaet Berlin 2002-2023.
6 //
7 // This software is released under a three-clause BSD license:
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of any author or any participating institution
14 // may be used to endorse or promote products derived from this software
15 // without specific prior written permission.
16 // For a full list of authors, refer to the file AUTHORS.
17 // --------------------------------------------------------------------------
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 // ARE DISCLAIMED. IN NO EVENT SHALL ANY OF THE AUTHORS OR THE CONTRIBUTING
22 // INSTITUTIONS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // --------------------------------------------------------------------------
31 // $Maintainer: Chris Bielow $
32 // $Authors: Marc Sturm, Chris Bielow $
33 // --------------------------------------------------------------------------
34 
35 #pragma once
36 
37 #include <OpenMS/config.h>
38 
39 #include <OpenMS/CONCEPT/Types.h>
41 
42 #ifdef OPENMS_HAS_SYS_TIME_H
43 #include <sys/time.h>
44 #endif
45 
46 #ifdef OPENMS_HAS_TIME_H
47 #include <ctime>
48 #endif
49 
50 namespace OpenMS
51 {
65  class OPENMS_DLLAPI StopWatch
66  {
67 public:
71 
81  void start();
82 
89  void stop();
90 
96  void resume();
97 
104  void reset();
105 
110  void clear();
111 
113 
117 
121  double getClockTime() const;
122 
126  double getUserTime() const;
127 
131  double getSystemTime() const;
132 
137  double getCPUTime() const;
138 
142 
146  bool isRunning() const;
147 
155  bool operator==(const StopWatch & stop_watch) const;
156 
164  bool operator!=(const StopWatch & stop_watch) const;
165 
172  bool operator<(const StopWatch & stop_watch) const;
173 
180  bool operator<=(const StopWatch & stop_watch) const;
181 
188  bool operator>=(const StopWatch & stop_watch) const;
189 
196  bool operator>(const StopWatch & stop_watch) const;
197 
199 
207  String toString() const;
208 
212  static String toString(const double time_in_seconds);
213 
214 private:
215  #ifdef OPENMS_WINDOWSPLATFORM
216  typedef OPENMS_UINT64_TYPE TimeType;
217  static const long long SecondsTo100Nano_;
218  #else
219  typedef clock_t TimeType;
220  static const PointerSizeInt cpu_speed_;
221  #endif
222 
223  struct TimeDiff_
224  {
225  TimeType user_ticks{ 0 };
226  TimeType kernel_ticks{ 0 };
227  PointerSizeInt start_time{ 0 };
228  PointerSizeInt start_time_usec{ 0 };
229 
230  double userTime() const;
231  double kernelTime() const;
232  double getCPUTime() const;
233  double clockTime() const;
234 
235  TimeDiff_ operator-(const TimeDiff_& earlier) const;
237  bool operator==(const TimeDiff_& rhs) const;
238 
239  private:
240  double ticksToSeconds_(TimeType in) const;
241  };
242 
243 
246 
250 
253 
255  bool is_running_ = false;
256 
257  };
258 
259 }
260 
261 
262 
This class is used to determine the current process' CPU (user and/or kernel) and wall time.
Definition: StopWatch.h:66
TimeDiff_ snapShot_() const
get the absolute times for current system, user and kernel times
static const PointerSizeInt cpu_speed_
POSIX API returns CPU ticks, so we need to divide by CPU speed.
Definition: StopWatch.h:220
String toString() const
get a compact representation of the current time status.
double getUserTime() const
void resume()
Resume a stopped StopWatch.
bool operator<(const StopWatch &stop_watch) const
bool operator<=(const StopWatch &stop_watch) const
TimeDiff_ last_start_
point in time of last start()
Definition: StopWatch.h:252
void start()
Start the stop watch.
static String toString(const double time_in_seconds)
void stop()
Stop the stop watch (can be resumed later). If the stop watch was not running an exception is thrown.
double getClockTime() const
TimeDiff_ accumulated_times_
Definition: StopWatch.h:249
clock_t TimeType
Definition: StopWatch.h:219
bool operator!=(const StopWatch &stop_watch) const
void reset()
Clear the stop watch but keep running.
bool isRunning() const
double getCPUTime() const
bool operator>(const StopWatch &stop_watch) const
bool operator>=(const StopWatch &stop_watch) const
bool operator==(const StopWatch &stop_watch) const
double getSystemTime() const
A more convenient string class.
Definition: String.h:60
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:48
Definition: StopWatch.h:224
TimeDiff_ & operator+=(const TimeDiff_ &other)
double ticksToSeconds_(TimeType in) const
TimeDiff_ operator-(const TimeDiff_ &earlier) const
bool operator==(const TimeDiff_ &rhs) const