OpenMS
StopWatch.h
Go to the documentation of this file.
1 // Copyright (c) 2002-2023, The OpenMS Team -- EKU Tuebingen, ETH Zurich, and FU Berlin
2 // SPDX-License-Identifier: BSD-3-Clause
3 //
4 // --------------------------------------------------------------------------
5 // $Maintainer: Chris Bielow $
6 // $Authors: Marc Sturm, Chris Bielow $
7 // --------------------------------------------------------------------------
8 
9 #pragma once
10 
11 #include <OpenMS/config.h>
12 
13 #include <OpenMS/CONCEPT/Types.h>
15 
16 #ifdef OPENMS_HAS_SYS_TIME_H
17 #include <sys/time.h>
18 #endif
19 
20 #ifdef OPENMS_HAS_TIME_H
21 #include <ctime>
22 #endif
23 
24 namespace OpenMS
25 {
39  class OPENMS_DLLAPI StopWatch
40  {
41 public:
45 
55  void start();
56 
63  void stop();
64 
70  void resume();
71 
78  void reset();
79 
84  void clear();
85 
87 
91 
95  double getClockTime() const;
96 
100  double getUserTime() const;
101 
105  double getSystemTime() const;
106 
111  double getCPUTime() const;
112 
116 
120  bool isRunning() const;
121 
129  bool operator==(const StopWatch & stop_watch) const;
130 
138  bool operator!=(const StopWatch & stop_watch) const;
139 
146  bool operator<(const StopWatch & stop_watch) const;
147 
154  bool operator<=(const StopWatch & stop_watch) const;
155 
162  bool operator>=(const StopWatch & stop_watch) const;
163 
170  bool operator>(const StopWatch & stop_watch) const;
171 
173 
181  String toString() const;
182 
186  static String toString(const double time_in_seconds);
187 
188 private:
189  #ifdef OPENMS_WINDOWSPLATFORM
190  typedef OPENMS_UINT64_TYPE TimeType;
191  static const long long SecondsTo100Nano_;
192  #else
193  typedef clock_t TimeType;
194  static const PointerSizeInt cpu_speed_;
195  #endif
196 
197  struct TimeDiff_
198  {
199  TimeType user_ticks{ 0 };
200  TimeType kernel_ticks{ 0 };
201  PointerSizeInt start_time{ 0 };
202  PointerSizeInt start_time_usec{ 0 };
203 
204  double userTime() const;
205  double kernelTime() const;
206  double getCPUTime() const;
207  double clockTime() const;
208 
209  TimeDiff_ operator-(const TimeDiff_& earlier) const;
211  bool operator==(const TimeDiff_& rhs) const;
212 
213  private:
214  double ticksToSeconds_(TimeType in) const;
215  };
216 
217 
220 
224 
227 
229  bool is_running_ = false;
230 
231  };
232 
233 }
234 
235 
236 
This class is used to determine the current process' CPU (user and/or kernel) and wall time.
Definition: StopWatch.h:40
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:194
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:226
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:223
clock_t TimeType
Definition: StopWatch.h:193
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:34
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:22
Definition: StopWatch.h:198
TimeDiff_ & operator+=(const TimeDiff_ &other)
double ticksToSeconds_(TimeType in) const
TimeDiff_ operator-(const TimeDiff_ &earlier) const
bool operator==(const TimeDiff_ &rhs) const