Arcane  4.1.12.0
User documentation
Loading...
Searching...
No Matches
Timer.h
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2026 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
4// See the top-level COPYRIGHT file for details.
5// SPDX-License-Identifier: Apache-2.0
6//-----------------------------------------------------------------------------
7/*---------------------------------------------------------------------------*/
8/* Timer.h (C) 2000-2025 */
9/* */
10/* Management of a timer. */
11/*---------------------------------------------------------------------------*/
12#ifndef ARCANE_CORE_TIMER_H
13#define ARCANE_CORE_TIMER_H
14/*---------------------------------------------------------------------------*/
15/*---------------------------------------------------------------------------*/
16
17#include "arcane/utils/String.h"
18
20
21/*---------------------------------------------------------------------------*/
22/*---------------------------------------------------------------------------*/
23
24namespace Arcane
25{
26
27/*---------------------------------------------------------------------------*/
28/*---------------------------------------------------------------------------*/
29
30class ITimerMng;
31class ITimeStats;
32
33/*---------------------------------------------------------------------------*/
34/*---------------------------------------------------------------------------*/
35
36/*!
37 * \brief Management of a timer.
38
39 * An instance of this class allows measuring the time elapsed between its
40 * activation via the start() method and its stopping via the stop() method.
41
42 * The timer can be used multiple times, and it is possible to
43 * know both the number of activations (nbActivated()) and the total time
44 * spent in its successive activations (totalTime()).
45
46 * There are two operating modes:
47 * <ul>
48 * <li>#TimerVirtual: the timer uses the CPU time of the process. This time
49 * is constant regardless of the machine load;</li>
50 * <li>#TimerReal: the timer uses real time. The resolution of this timer is
51 * generally better than with the previous type, but it is only significant
52 * when the machine is dedicated to the process.</li>
53 * </ul>
54 *
55 * \note Since version 3.6 of %Arcane, #TimerVirtual is obsolete and
56 * the returned value will be equivalent to #TimerReal.
57 *
58* The timer resolution depends on the machine. It is on the order of
59* milliseconds for timers using CPU time and on the order of
60* microseconds for timers using real time.
61 */
62class ARCANE_CORE_EXPORT Timer
63{
64 public:
65
66 //! Timer type
68 {
69 /*!
70 * \brief Timer using CPU time (obsolete).
71 *
72 * \deprecated This timer is no longer used and behaves like
73 * the clock time (TimerReal).
74 */
76 //! Timer using real time
78 };
79
80 public:
81
82 /*!
83 * \brief Sentinel for the timer.
84 * The sentinel associated with a timer allows it to be triggered
85 * upon its construction and stopped upon its
86 * destruction. This ensures that the timer will be properly stopped in case
87 * of an exception, for example.
88 */
89 class ARCANE_CORE_EXPORT Sentry
90 {
91 public:
92
93 //! Associates the timer \a t and starts it
95 : m_timer(t)
96 {
97 m_timer->start();
98 }
99 //! Stops the associated timer
101 {
102 m_timer->stop();
103 }
104
105 private:
106
107 Timer* m_timer; //!< Associated timer
108 };
109
110 /*!
111 * \brief Positions the name of the currently executing action.
112 *
113 * An action name can be anything. It is
114 * just used to differentiate the different parts of a
115 * run and to know the time of each one.
116 * Actions must be nested within each other
117 */
118 class ARCANE_CORE_EXPORT Action
119 {
120 public:
121
122 Action(ISubDomain* sub_domain, const String& action_name, bool print_time = false);
123 Action(ITimeStats* stats, const String& action_name, bool print_time = false);
124 ~Action();
125
126 public:
127 private:
128
129 ITimeStats* m_stats;
130 String m_action_name;
131 bool m_print_time;
132
133 private:
134
135 void _init();
136 };
137
138 /*!
139 * \brief Positions the phase of the currently executing action.
140 */
141 class ARCANE_CORE_EXPORT Phase
142 {
143 public:
144 public:
145
146 Phase(ISubDomain* sub_domain, eTimePhase pt);
147 Phase(ITimeStats* stats, eTimePhase pt);
148 ~Phase();
149
150 public:
151 private:
152
153 ITimeStats* m_stats; //!< Sub-domain manager
154 eTimePhase m_phase_type;
155
156 private:
157
158 void _init();
159 };
160
161 /*!
162 * \brief Displays the time elapsed between the call to the constructor and the destructor.
163 *
164 * This class allows simply displaying, at the time of the destructor,
165 * the real time elapsed since the call to the constructor. The display is done
166 * via the info() method of ITraceMng.
167 * \code
168 * {
169 * Timer::SimplePrinter sp(traceMng(),"myFunction");
170 * myFunction();
171 * }
172 * \endcode
173 */
174 class ARCANE_CORE_EXPORT SimplePrinter
175 {
176 public:
177
178 SimplePrinter(ITraceMng* tm, const String& msg);
179 SimplePrinter(ITraceMng* tm, const String& msg, bool is_active);
180 ~SimplePrinter();
181
182 private:
183
184 ITraceMng* m_trace_mng;
185 Real m_begin_time;
186 bool m_is_active;
187 String m_message;
188
189 private:
190
191 void _init();
192 };
193
194 public:
195
196 /*!
197 * \brief Constructs a timer.
198 *
199 * Constructs a timer linked to the sub-domain \a sd, with name \a name and
200 * type \a type.
201 */
203
204 /*!
205 * \brief Constructs a timer.
206 *
207 * Constructs a timer linked to the manager \a tm, with name \a name and
208 * type \a type.
209 */
211
212 ~Timer(); //!< Frees resources
213
214 public:
215
216 /*!
217 * \brief Activates the timer.
218 *
219 * If the timer is already active, this method does nothing.
220 */
221 void start();
222
223 /*!
224 * \brief Deactivates the timer.
225 *
226 * If the timer is not active at the time of the call, this method does not
227 * do anything.
228 *
229 * \return the time elapsed (in seconds) since the last activation.
230 */
231 Real stop();
232
233 //! Returns the activation status of the timer
234 bool isActivated() const { return m_is_activated; }
235
236 //! Returns the name of the timer
237 const String& name() const { return m_name; }
238
239 //! Returns the total time (in seconds) spent in the timer
240 Real totalTime() const { return m_total_time; }
241
242 //! Returns the time (in seconds) spent during the last activation of the timer
243 Real lastActivationTime() const { return m_activation_time; }
244
245 //! Returns the number of times the timer has been activated
246 Integer nbActivated() const { return m_nb_activated; }
247
248 //! Returns the type of time used
249 eTimerType type() const { return m_type; }
250
251 //! Resets the time counters
252 void reset();
253
254 //! Manager associated with this timer.
255 ITimerMng* timerMng() const { return m_timer_mng; }
256
257 public:
258
259 static TimeMetricAction phaseAction(ITimeStats* s, eTimePhase phase);
260
261 public:
262
263 //! \internal
264 void _setStartTime(Real t) { m_start_time = t; }
265 //! \internal
266 Real _startTime() const { return m_start_time; }
267
268 private:
269
270 ITimerMng* m_timer_mng; //!< Timer manager
271 eTimerType m_type; //!< Timer type
272 Integer m_nb_activated; //!< Number of times the timer has been activated
273 bool m_is_activated; //!< \a true if the timer is active
274 Real m_activation_time; //!< Time spent during the last activation
275 Real m_total_time; //!< Total time spent in the timer
276 String m_name; //!< Timer name
277 Real m_start_time; //!< Time of the start of the last activation
278};
279
280/*---------------------------------------------------------------------------*/
281/*---------------------------------------------------------------------------*/
282
283} // End namespace Arcane
284
285/*---------------------------------------------------------------------------*/
286/*---------------------------------------------------------------------------*/
287
288#endif
Declarations of Arcane's general types.
Interface of the subdomain manager.
Definition ISubDomain.h:75
Interface of a timer manager.
Definition ITimerMng.h:50
Sentry(Timer *t)
Associates the timer t and starts it.
Definition Timer.h:94
~Sentry()
Stops the associated timer.
Definition Timer.h:100
const String & name() const
Returns the name of the timer.
Definition Timer.h:237
Integer nbActivated() const
Returns the number of times the timer has been activated.
Definition Timer.h:246
ITimerMng * timerMng() const
Manager associated with this timer.
Definition Timer.h:255
Real stop()
Deactivates the timer.
Definition Timer.cc:87
eTimerType
Timer type.
Definition Timer.h:68
@ TimerReal
Timer using real time.
Definition Timer.h:77
@ TimerVirtual
Timer using CPU time (obsolete).
Definition Timer.h:75
eTimerType type() const
Returns the type of time used.
Definition Timer.h:249
Timer(ISubDomain *sd, const String &name, eTimerType type)
Constructs a timer.
Definition Timer.cc:37
Real totalTime() const
Returns the total time (in seconds) spent in the timer.
Definition Timer.h:240
bool isActivated() const
Returns the activation status of the timer.
Definition Timer.h:234
void start()
Activates the timer.
Definition Timer.cc:70
Real lastActivationTime() const
Returns the time (in seconds) spent during the last activation of the timer.
Definition Timer.h:243
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
Int32 Integer
Type representing an integer.
double Real
Type representing a real number.
eTimePhase
Phase of a temporal action.