Clock.h
1 /* This file is part of the Gudhi Library. The Gudhi library
2  * (Geometric Understanding in Higher Dimensions) is a generic C++
3  * library for computational topology.
4  *
5  * Author(s): David Salinas
6  *
7  * Copyright (C) 2014 Inria
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program 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
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <http://www.gnu.org/licenses/>.
21  */
22 
23 #ifndef CLOCK_H_
24 #define CLOCK_H_
25 
26 #include <iostream>
27 #include <string>
28 #include <chrono>
29 
30 namespace Gudhi {
31 
32 class Clock {
33  public:
34  // Construct and start the timer
35  Clock(const std::string& msg_ = std::string())
36  : startTime(std::chrono::system_clock::now()),
37  end_called(false),
38  msg(msg_) { }
39 
40  // Restart the timer
41  void begin() const {
42  end_called = false;
43  startTime = std::chrono::system_clock::now();
44  }
45 
46  // Stop the timer
47  void end() const {
48  end_called = true;
49  endTime = std::chrono::system_clock::now();
50  }
51 
52  std::string message() const {
53  return msg;
54  }
55 
56  // Print current value to std::cout
57  void print() const {
58  std::cout << *this << std::endl;
59  }
60 
61  friend std::ostream& operator<<(std::ostream& stream, const Clock& clock) {
62  if (!clock.msg.empty())
63  stream << clock.msg << ": ";
64 
65  stream << clock.num_seconds() << "s\n";
66  return stream;
67  }
68 
69  // Get the number of seconds between the timer start and:
70  // - the last call of end() if it was called
71  // - or now otherwise. In this case, the timer is not stopped.
72  double num_seconds() const {
73  if (!end_called) {
74  auto end = std::chrono::system_clock::now();
75  return std::chrono::duration_cast<std::chrono::milliseconds>(end-startTime).count() / 1000.;
76  } else {
77  return std::chrono::duration_cast<std::chrono::milliseconds>(endTime-startTime).count() / 1000.;
78  }
79  }
80 
81  private:
82  mutable std::chrono::time_point<std::chrono::system_clock> startTime, endTime;
83  mutable bool end_called;
84  std::string msg;
85 };
86 
87 } // namespace Gudhi
88 
89 #endif // CLOCK_H_
Definition: SimplicialComplexForAlpha.h:26
GUDHI  Version 2.3.0  - C++ library for Topological Data Analysis (TDA) and Higher Dimensional Geometry Understanding.  - Copyright : GPL v3 Generated on Fri Oct 18 2019 18:40:54 for GUDHI by Doxygen 1.8.13