0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Stopwatch.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2007-2015 Hypertable, Inc.
3  *
4  * This file is part of Hypertable.
5  *
6  * Hypertable is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 3
9  * of the License, or any later version.
10  *
11  * Hypertable is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  * 02110-1301, USA.
20  */
21 
24 
25 #ifndef Common_Stopwatch_h
26 #define Common_Stopwatch_h
27 
28 #include <chrono>
29 #include <cstring>
30 
31 namespace Hypertable {
32 
35 
40  class Stopwatch {
41  public:
44  Stopwatch(bool start_running = true) {
45  if (start_running)
46  start();
47  }
48 
50  void start() {
51  if (!m_running) {
52  start_time = std::chrono::steady_clock::now();
53  m_running = true;
54  }
55  }
56 
58  void stop() {
59  if (m_running) {
60  elapsed_time += std::chrono::steady_clock::now() - start_time;
61  m_running = false;
62  }
63  }
64 
66  void reset() {
67  elapsed_time = std::chrono::steady_clock::duration::zero();
68  }
69 
72  double elapsed() {
73  if (m_running) {
74  stop();
75  start();
76  }
77  return ((double)elapsed_time.count() *
78  std::chrono::steady_clock::duration::period::num) /
79  (double)std::chrono::steady_clock::duration::period::den;
80  }
81 
84  int64_t elapsed_millis() {
85  if (m_running) {
86  stop();
87  start();
88  }
89  return std::chrono::duration_cast<std::chrono::milliseconds>(elapsed_time).count();
90  }
91 
92  private:
94  bool m_running {};
95 
97  std::chrono::steady_clock::time_point start_time;
98 
100  std::chrono::steady_clock::duration elapsed_time {0};
101  };
102 
104 
105 }
106 
107 #endif // Common_Stopwatch_h
double elapsed()
Returns the elapsed time.
Definition: Stopwatch.h:72
std::chrono::steady_clock::time_point start_time
The start time.
Definition: Stopwatch.h:97
std::chrono::steady_clock::duration elapsed_time
The elapsed time.
Definition: Stopwatch.h:100
void start()
Starts the Stopwatch.
Definition: Stopwatch.h:50
int64_t elapsed_millis()
Returns elapsed time in milliseconds.
Definition: Stopwatch.h:84
Hypertable definitions
void reset()
Resets the Stopwatch.
Definition: Stopwatch.h:66
The Stopwatch class measures elapsed time between instantiation (or a call to start) and a call to st...
Definition: Stopwatch.h:40
bool m_running
Flag whether the Stopwatch is currently running.
Definition: Stopwatch.h:94
Stopwatch(bool start_running=true)
Constructor; if start_running is true then the Stopwatch is started immediately.
Definition: Stopwatch.h:44
void stop()
Stops the Stopwatch.
Definition: Stopwatch.h:58