1 /**
2  *
3  *   Copyright (c) 2005-2021 by Pierre-Henri WUILLEMIN(_at_LIP6) & Christophe GONZALES(_at_AMU)
4  *   info_at_agrum_dot_org
5  *
6  *  This library is free software: you can redistribute it and/or modify
7  *  it under the terms of the GNU Lesser General Public License as published by
8  *  the Free Software Foundation, either version 3 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This library 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 Lesser General Public License for more details.
15  *
16  *  You should have received a copy of the GNU Lesser General Public License
17  *  along with this library.  If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 
22 /**
23  * @file
24  * @brief Class used to compute response times for benchmark purposes.
25  *
26  * @author Pierre-Henri WUILLEMIN(_at_LIP6) & Jean-Philippe Dubus
27  */
28 #ifndef GUM_TIMER_H
29 #define GUM_TIMER_H
30 #include <chrono>
31 
32 #include <agrum/agrum.h>
33 
34 
35 namespace gum {
36 
37   /**
38    * @class Timer
39    * @headerfile timer.h <agrum/tools/core/timer.h>
40    * @brief Class used to compute response times for benchmark purposes
41    * @ingroup basicstruct_group
42    *
43    * This class represents a classic timer, it starts in the constructor, you
44    * can reset it with method reset() and you can get the delta time with the
45    * method step().
46    *
47    * This class uses double for representing time data, all the values are in
48    * seconds, and the precision is about 0.001 s
49    */
50   class Timer {
51     public:
52     // ============================================================================
53     /// @name Constructors / Destructors
54     // ============================================================================
55     /// @{
56 
57     /**
58      * @brief Default constructor (launching the timer).
59      */
60     Timer();
61 
62     /**
63      * @brief Copy constructor.
64      * @param timer The gum::Timer to copy.
65      */
66     Timer(const Timer& timer);
67 
68     /**
69      * Destructor
70      */
71     ~Timer();
72 
73     /// @}
74     // ============================================================================
75     /// @name Operators
76     // ============================================================================
77     /// @{
78 
79     /**
80      * @brief Copy operator.
81      * @param timer The gum::Timer to copy.
82      */
83     Timer& operator=(const Timer& timer);
84 
85     /// @}
86     // ============================================================================
87     /// @name Timer manipulation
88     // ============================================================================
89     /// @{
90 
91     /**
92      * @brief Reset the timer.
93      */
94     void reset();
95     /**
96      * @brief Pause the timer and return the delta (@see gum::Timer::step() ).
97      * @brief Returns the delta (@see gum::Timer::step() ).
98      */
99     double pause();
100     /**
101      * @brief Resume the timer and return the delta (@see gum::Timer::step() ).
102      * @brief Returns the delta (@see gum::Timer::step() ).
103      */
104     double resume();
105 
106     /**
107      * @brief Returns the delta time between now and the last reset() call (or
108      * the constructor).
109      *
110      * @return Returns the delta time in seconds (accuracy : 0.001 ms).
111      */
112     double step() const;
113 
114     /// @}
115 
116     protected:
117     /// Time of the last call to reset() or the constructor.
118     std::chrono::high_resolution_clock::time_point start_;
119 
120     /// Time of the last call to pause().
121     std::chrono::high_resolution_clock::time_point pause_;
122 
123     /// False if running.
124     bool sleeping_;
125   };
126 
127 } /* namespace gum */
128 
129 #ifndef GUM_NO_INLINE
130 #  include <agrum/tools/core/timer_inl.h>
131 #endif   // GUM_NO_INLINE
132 
133 #endif   // GUM_TIMER_H
134