Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
ahNow.H
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
32
44# ifndef AHNOW_H
45# define AHNOW_H
46
47# include <string>
48# include <chrono>
49
50# include <aleph.H>
51
52namespace Aleph
53{
78 class Now
79 {
80 public:
82 enum class Precision
83 {
84 Hours,
85 Minutes,
86 Seconds,
90 };
91
92 static int to_int(Precision p)
93 {
94 return static_cast<int>(p);
95 }
96
97 using ClockType = std::chrono::high_resolution_clock;
98 using TimePointType = ClockType::time_point;
99 using DurationType = ClockType::duration;
100
101 private:
103
105
106 static const double precision_values[];
107
108 public:
111 {
112 return ClockType::now();
113 }
114
123 static double compute_time_diff(const TimePointType & rtp,
124 const TimePointType & ltp,
125 const Precision & precision)
126 {
127 DurationType et = std::chrono::duration_cast<std::chrono::nanoseconds>(rtp - ltp);
128 return et.count() * precision_values[to_int(precision)];
129 }
130
143 Now(bool start_now = false)
145 {
146 if (start_now)
147 start();
148 }
149
162 Now(const Precision & _precision, bool start_now = false)
164 {
165 if (start_now)
166 start();
167 }
168
173 const Precision & get_precision() const
174 {
175 return precision;
176 }
177
183 {
185 }
186
196 {
197 tp = this->current_time_point();
198 return tp;
199 }
200
209 double elapsed()
210 {
212 tp = this->current_time_point();
213 return compute_time_diff(tp, ltp, precision);
214 }
215
220 double delta()
221 {
222 return elapsed();
223 }
224
236 static double elapsed(const TimePointType & tp,
238 {
240 }
241
248 static double delta(const TimePointType & tp,
250 {
251 return elapsed(tp, precision);
252 }
253 };
254} // End namespace Aleph
255
256# endif // AHNOW_H
Core header for the Aleph-w library.
Class Now is a practical class for timing based in a high resolution clock.
Definition ahNow.H:79
ClockType::time_point TimePointType
Definition ahNow.H:98
static double compute_time_diff(const TimePointType &rtp, const TimePointType &ltp, const Precision &precision)
Calculates the time that has elapsed between two time points.
Definition ahNow.H:123
TimePointType start()
Sets internally the current time point.
Definition ahNow.H:195
TimePointType tp
Definition ahNow.H:102
std::chrono::high_resolution_clock ClockType
Definition ahNow.H:97
Precision precision
Definition ahNow.H:104
static const double precision_values[]
Definition ahNow.H:35
const Precision & get_precision() const
Gets the type of precision.
Definition ahNow.H:173
double delta()
Like elapsed().
Definition ahNow.H:220
static int to_int(Precision p)
Definition ahNow.H:92
Precision
Precision for timing.
Definition ahNow.H:83
void set_precision(const Precision &_precision)
Sets the type of precision.
Definition ahNow.H:182
Now(bool start_now=false)
Builds a new object with default values.
Definition ahNow.H:143
ClockType::duration DurationType
Definition ahNow.H:99
static TimePointType current_time_point()
Gets the current time point.
Definition ahNow.H:110
Now(const Precision &_precision, bool start_now=false)
Builds a new object with parametric precision time.
Definition ahNow.H:162
static double delta(const TimePointType &tp, const Precision &precision=Precision::Milliseconds)
Like elapsed(const TimePointType & tp, const Precision & precision = MILLISECONDS).
Definition ahNow.H:248
double elapsed()
Calculates the time that has elapsed since the last time start(), elapsed() or delta() was called.
Definition ahNow.H:209
static double elapsed(const TimePointType &tp, const Precision &precision=Precision::Milliseconds)
Calculates the time that has elapsed since a given time point.
Definition ahNow.H:236
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
DynList< T > maps(const C &c, Op op)
Classic map operation.