ʻOhana
Population structure, admixture history, and selection using learning methods.
jade.stopwatch.hpp
1 /* -------------------------------------------------------------------------
2  Ohana
3  Copyright (c) 2015-2020 Jade Cheng (\___/)
4  Jade Cheng <info@jade-cheng.com> (='.'=)
5  ------------------------------------------------------------------------- */
6 
7 #ifndef JADE_STOPWATCH_HPP__
8 #define JADE_STOPWATCH_HPP__
9 
10 #include "jade.system.hpp"
11 
12 namespace jade
13 {
14  ///
15  /// A template class imlementing a stopwatch.
16  ///
17  template <typename TClock>
19  {
20  public:
21  /// The clock type.
22  typedef TClock clock_type;
23 
24  /// The time point type for the clock.
25  typedef typename clock_type::time_point time_point_type;
26 
27  ///
28  /// Initializes a new instance of the class.
29  ///
30  inline basic_stopwatch()
31  : _t0 (clock_type::now())
32  {
33  }
34 
35  ///
36  /// Compares the elapsed time with the specified number of seconds. The
37  /// method returns a negative value if the elapsed time is lesser.
38  ///
39  /// \return Negative, positive, or zero.
40  ///
41  int compare(
42  const double seconds) ///< The elapsed seconds to compare.
43  const
44  {
45  const auto lhs = get_elapsed();
46  const auto rhs = seconds;
47  return lhs < rhs ? -1 : rhs < lhs ? +1 : 0;
48  }
49 
50  ///
51  /// \return The elapsed time in seconds as a floating-point value.
52  ///
53  double get_elapsed() const
54  {
55  const auto t1 = clock_type::now();
56  const auto dt = std::chrono::duration<double>(t1 - _t0);
57  return dt.count();
58  }
59 
60  ///
61  /// \return The current time as a string.
62  ///
63  std::string str() const
64  {
65  std::ostringstream out;
66  out << std::fixed << std::setprecision(6) << get_elapsed();
67  return out.str();
68  }
69 
70  ///
71  /// Compares the elapsed time with the specified number of seconds. The
72  /// operator returns a negative value if the elapsed time is lesser.
73  ///
74  /// \return True if the value is lesser; otherwise false.
75  ///
76  inline bool operator < (
77  const double rhs) ///< The elapsed seconds to compare.
78  const
79  {
80  return compare(rhs) < 0;
81  }
82 
83  ///
84  /// Compares the elapsed time with the specified number of seconds. The
85  /// operator returns a negative value if the elapsed time is greater.
86  ///
87  /// \return True if the value is greater; otherwise false.
88  ///
89  inline bool operator > (
90  const double rhs) ///< The elapsed seconds to compare.
91  const
92  {
93  return compare(rhs) > 0;
94  }
95 
96  private:
97  time_point_type _t0;
98  };
99 
100  /// A class implementing a stopwatch.
101  typedef basic_stopwatch<std::chrono::high_resolution_clock> stopwatch;
102 }
103 
104 ///
105 /// Writes the value of a stopwatch to an output stream.
106 ///
107 /// \return The output stream.
108 ///
109 template <typename TClock>
110 std::ostream & operator << (
111  std::ostream & dst, ///< The output stream.
112  const jade::basic_stopwatch<TClock> & src) ///< The stopwatch.
113 {
114  return dst << src.str();
115 }
116 
117 #endif // JADE_STOPWATCH_HPP__
jade::basic_stopwatch::operator>
bool operator>(const double rhs) const
Compares the elapsed time with the specified number of seconds. The operator returns a negative value...
Definition: jade.stopwatch.hpp:89
jade::basic_stopwatch::operator<
bool operator<(const double rhs) const
Compares the elapsed time with the specified number of seconds. The operator returns a negative value...
Definition: jade.stopwatch.hpp:76
jade::basic_stopwatch::get_elapsed
double get_elapsed() const
Definition: jade.stopwatch.hpp:53
jade::basic_stopwatch::clock_type
TClock clock_type
The clock type.
Definition: jade.stopwatch.hpp:22
jade::basic_stopwatch
A template class imlementing a stopwatch.
Definition: jade.stopwatch.hpp:19
jade::basic_stopwatch::basic_stopwatch
basic_stopwatch()
Initializes a new instance of the class.
Definition: jade.stopwatch.hpp:30
jade::basic_stopwatch::compare
int compare(const double seconds) const
Compares the elapsed time with the specified number of seconds. The method returns a negative value i...
Definition: jade.stopwatch.hpp:41
jade::basic_stopwatch::str
std::string str() const
Definition: jade.stopwatch.hpp:63
jade::basic_stopwatch::time_point_type
clock_type::time_point time_point_type
The time point type for the clock.
Definition: jade.stopwatch.hpp:25