Aleph-w  1.9
General library for algorithms and data structures
ahNow.H
1 
2 /* Aleph-w
3 
4  / \ | | ___ _ __ | |__ __ __
5  / _ \ | |/ _ \ '_ \| '_ \ ____\ \ /\ / / Data structures & Algorithms
6  / ___ \| | __/ |_) | | | |_____\ V V / version 1.9b
7  /_/ \_\_|\___| .__/|_| |_| \_/\_/ https://github.com/lrleon/Aleph-w
8  |_|
9 
10  This file is part of Aleph-w library
11 
12  Copyright (c) 2002-2018 Leandro Rabindranath Leon & Alejandro Mujica
13 
14  This program is free software: you can redistribute it and/or modify
15  it under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  This program is distributed in the hope that it will be useful, but
20  WITHOUT ANY WARRANTY; without even the implied warranty of
21  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  General Public License for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with this program. If not, see <https://www.gnu.org/licenses/>.
26 */
27 # ifndef AHNOW_H
28 # define AHNOW_H
29 
30 # include <string>
31 # include <chrono>
32 
33 # include <aleph.H>
34 
35 using namespace std;
36 using namespace chrono;
37 using namespace Aleph;
38 
39 namespace Aleph
40 {
65  class Now
66  {
67  public:
69  enum class Precision
70  {
71  Hours,
72  Minutes,
73  Seconds,
74  Milliseconds,
75  Microseconds,
76  Nanoseconds
77  };
78 
79  static int to_int(Precision p)
80  {
81  return static_cast<int>(p);
82  }
83 
84  using ClockType = high_resolution_clock;
85  using TimePointType = ClockType::time_point;
86  using DurationType = ClockType::duration;
87 
88  private:
89  TimePointType tp;
90 
91  Precision precision;
92 
93  static const double precision_values[];
94 
95  public:
97  static TimePointType current_time_point()
98  {
99  return ClockType::now();
100  }
101 
110  static double compute_time_diff(const TimePointType & rtp,
111  const TimePointType & ltp,
112  const Precision & precision)
113  {
114  DurationType et = std::chrono::duration_cast<nanoseconds>(rtp - ltp);
115  return et.count() * precision_values[to_int(precision)];
116  }
117 
130  Now(bool start_now = false)
131  : tp(), precision(Precision::Milliseconds)
132  {
133  if (start_now)
134  start();
135  }
136 
149  Now(const Precision & _precision, bool start_now = false)
150  : tp(), precision(_precision)
151  {
152  if (start_now)
153  start();
154  }
155 
160  const Precision & get_precision() const
161  {
162  return precision;
163  }
164 
169  void set_precision(const Precision & _precision)
170  {
171  precision = _precision;
172  }
173 
182  TimePointType start()
183  {
184  tp = this->current_time_point();
185  return tp;
186  }
187 
196  double elapsed()
197  {
198  TimePointType ltp = tp;
199  tp = this->current_time_point();
200  return compute_time_diff(tp, ltp, precision);
201  }
202 
207  double delta()
208  {
209  return elapsed();
210  }
211 
223  static double elapsed(const TimePointType & tp,
224  const Precision & precision = Precision::Milliseconds)
225  {
226  return compute_time_diff(current_time_point(), tp, precision);
227  }
228 
235  static double delta(const TimePointType & tp,
236  const Precision & precision = Precision::Milliseconds)
237  {
238  return elapsed(tp, precision);
239  }
240  };
241 } // End namespace Aleph
242 
243 # endif // AHNOW_H
double elapsed()
Definition: ahNow.H:196
static double delta(const TimePointType &tp, const Precision &precision=Precision::Milliseconds)
Definition: ahNow.H:235
TimePointType start()
Definition: ahNow.H:182
double delta()
Definition: ahNow.H:207
Definition: ah-comb.H:35
static double compute_time_diff(const TimePointType &rtp, const TimePointType &ltp, const Precision &precision)
Definition: ahNow.H:110
static TimePointType current_time_point()
Gets the current time point.
Definition: ahNow.H:97
Precision
Precision for timing.
Definition: ahNow.H:69
Definition: ahNow.H:65
void set_precision(const Precision &_precision)
Definition: ahNow.H:169
Now(bool start_now=false)
Builds a new object with default values.
Definition: ahNow.H:130
const Precision & get_precision() const
Definition: ahNow.H:160
Now(const Precision &_precision, bool start_now=false)
Builds a new object with parametric precision time.
Definition: ahNow.H:149
static double elapsed(const TimePointType &tp, const Precision &precision=Precision::Milliseconds)
Definition: ahNow.H:223

Leandro Rabindranath León