MagickCore 7.0.10
timer.c
Go to the documentation of this file.
1/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% %
6% TTTTT IIIII M M EEEEE RRRR %
7% T I MM MM E R R %
8% T I M M M EEE RRRR %
9% T I M M E R R %
10% T IIIII M M EEEEE R R %
11% %
12% %
13% Wizard's Toolkit Timing Methods %
14% %
15% Software Design %
16% Cristy %
17% January 1993 %
18% %
19% %
20% Copyright @ 1999 ImageMagick Studio LLC, a non-profit organization %
21% dedicated to making software imaging solutions freely available. %
22% %
23% You may not use this file except in compliance with the License. You may %
24% obtain a copy of the License at %
25% %
26% https://imagemagick.org/script/license.php %
27% %
28% Unless required by applicable law or agreed to in writing, software %
29% distributed under the License is distributed on an "AS IS" BASIS, %
30% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31% See the License for the specific language governing permissions and %
32% limitations under the License. %
33% %
34%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35%
36% Contributed by Bill Radcliffe and Bob Friesenhahn.
37%
38*/
39
40/*
41 Include declarations.
42*/
43#include "wizard/studio.h"
44#include "wizard/exception.h"
46#include "wizard/log.h"
47#include "wizard/memory_.h"
48#include "wizard/timer.h"
49
50/*
51 Typedef declarations.
52*/
54{
55 Timer
58
61
62 size_t
64};
65
66/*
67 Define declarations.
68*/
69#if !defined(CLOCKS_PER_SEC)
70#define CLOCKS_PER_SEC 100
71#endif
72
73/*
74 Forward declarations.
75*/
76static double
77 UserTime(void);
78
79static void
81
82/*
83%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
84% %
85% %
86% %
87% A c q u i r e T i m e r I n f o %
88% %
89% %
90% %
91%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
92%
93% AcquireTimerInfo() initializes the TimerInfo structure. It effectively
94% creates a stopwatch and starts it.
95%
96% The format of the AcquireTimerInfo method is:
97%
98% TimerInfo *AcquireTimerInfo(void)
99%
100*/
102{
104 *timer_info;
105
106 timer_info=(TimerInfo *) AcquireWizardMemory(sizeof(*timer_info));
107 if (timer_info == (TimerInfo *) NULL)
109 (void) memset(timer_info,0,sizeof(*timer_info));
110 timer_info->signature=WizardSignature;
111 GetTimerInfo(timer_info);
112 return(timer_info);
113}
114
115/*
116%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
117% %
118% %
119% %
120% C o n t i n u e T i m e r %
121% %
122% %
123% %
124%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
125%
126% ContinueTimer() resumes a stopped stopwatch. The stopwatch continues
127% counting from the last StartTimer() onwards.
128%
129% The format of the ContinueTimer method is:
130%
131% WizardBooleanType ContinueTimer(TimerInfo *timer_info)
132%
133% A description of each parameter follows.
134%
135% o timer_info: Time statistics structure.
136%
137*/
139{
140 assert(timer_info != (TimerInfo *) NULL);
141 assert(timer_info->signature == WizardSignature);
142 if (timer_info->state == UndefinedTimerState)
143 return(WizardFalse);
144 if (timer_info->state == StoppedTimerState)
145 {
146 timer_info->user.total-=timer_info->user.stop-timer_info->user.start;
147 timer_info->elapsed.total-=
148 timer_info->elapsed.stop-timer_info->elapsed.start;
149 }
150 timer_info->state=RunningTimerState;
151 return(WizardTrue);
152}
153
154/*
155%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
156% %
157% %
158% %
159% D e s t r o y T i m e r I n f o %
160% %
161% %
162% %
163%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
164%
165% DestroyTimerInfo() zeros memory associated with the TimerInfo structure.
166%
167% The format of the DestroyTimerInfo method is:
168%
169% TimerInfo *DestroyTimerInfo(TimerInfo *timer_info)
170%
171% A description of each parameter follows:
172%
173% o timer_info: The cipher context.
174%
175*/
177{
178 WizardAssert(CipherDomain,timer_info != (TimerInfo *) NULL);
180 timer_info->signature=(~WizardSignature);
181 timer_info=(TimerInfo *) RelinquishWizardMemory(timer_info);
182 return(timer_info);
183}
184
185/*
186%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
187% %
188% %
189% %
190+ E l a p s e d T i m e %
191% %
192% %
193% %
194%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
195%
196% ElapsedTime() returns the elapsed time (in seconds) since the last call to
197% StartTimer().
198%
199% The format of the ElapsedTime method is:
200%
201% double ElapsedTime()
202%
203*/
204static double ElapsedTime(void)
205{
206#if defined(WIZARDSTOOLKIT_HAVE_TIMES) && defined(WIZARDSTOOLKIT_HAVE_SYSCONF)
207 struct tms
208 timer;
209
210 return((double) times(&timer)/sysconf(_SC_CLK_TCK));
211#else
212#if defined(WIZARDSTOOLKIT_WINDOWS_SUPPORT)
213 return(NTElapsedTime());
214#else
215 return((double) clock()/CLOCKS_PER_SEC);
216#endif
217#endif
218}
219
220/*
221%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
222% %
223% %
224% %
225% G e t E l a p s e d T i m e %
226% %
227% %
228% %
229%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
230%
231% GetElapsedTime() returns the elapsed time (in seconds) passed between the
232% start and stop events. If the stopwatch is still running, it is stopped
233% first.
234%
235% The format of the GetElapsedTime method is:
236%
237% double GetElapsedTime(TimerInfo *timer_info)
238%
239% A description of each parameter follows.
240%
241% o timer_info: Timer statistics structure.
242%
243*/
245{
246 assert(timer_info != (TimerInfo *) NULL);
247 assert(timer_info->signature == WizardSignature);
248 if (timer_info->state == UndefinedTimerState)
249 return(0.0);
250 if (timer_info->state == RunningTimerState)
251 StopTimer(timer_info);
252 return(timer_info->elapsed.total);
253}
254
255/*
256%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
257% %
258% %
259% %
260+ G e t T i m e r I n f o %
261% %
262% %
263% %
264%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
265%
266% GetTimerInfo() initializes the TimerInfo structure. It effectively creates
267% a stopwatch and starts it.
268%
269% The format of the GetTimerInfo method is:
270%
271% void GetTimerInfo(TimerInfo *timer_info)
272%
273% A description of each parameter follows.
274%
275% o timer_info: Timer statistics structure.
276%
277*/
279{
280 /*
281 Create a stopwatch and start it.
282 */
283 assert(timer_info != (TimerInfo *) NULL);
284 timer_info->state=UndefinedTimerState;
285 StartTimer(timer_info,WizardTrue);
286}
287
288/*
289%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
290% %
291% %
292% %
293% G e t U s e r T i m e %
294% %
295% %
296% %
297%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
298%
299% GetUserTime() returns the User time (user and system) by the operating
300% system (in seconds) between the start and stop events. If the stopwatch is
301% still running, it is stopped first.
302%
303% The format of the GetUserTime method is:
304%
305% double GetUserTime(TimerInfo *timer_info)
306%
307% A description of each parameter follows.
308%
309% o timer_info: Timer statistics structure.
310%
311*/
313{
314 assert(timer_info != (TimerInfo *) NULL);
315 assert(timer_info->signature == WizardSignature);
316 if (timer_info->state == UndefinedTimerState)
317 return(0.0);
318 if (timer_info->state == RunningTimerState)
319 StopTimer(timer_info);
320 return(timer_info->user.total);
321}
322
323/*
324%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
325% %
326% %
327% %
328% R e s e t T i m e r %
329% %
330% %
331% %
332%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
333%
334% ResetTimer() resets the stopwatch.
335%
336% The format of the ResetTimer method is:
337%
338% void ResetTimer(TimerInfo *timer_info)
339%
340% A description of each parameter follows.
341%
342% o timer_info: Timer statistics structure.
343%
344*/
346{
347 assert(timer_info != (TimerInfo *) NULL);
348 assert(timer_info->signature == WizardSignature);
349 StopTimer(timer_info);
350 timer_info->elapsed.stop=0.0;
351 timer_info->user.stop=0.0;
352}
353
354/*
355%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
356% %
357% %
358% %
359+ S t a r t T i m e r %
360% %
361% %
362% %
363%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
364%
365% StartTimer() starts the stopwatch.
366%
367% The format of the StartTimer method is:
368%
369% void StartTimer(TimerInfo *timer_info,const WizardBooleanType reset)
370%
371% A description of each parameter follows.
372%
373% o timer_info: Timer statistics structure.
374%
375% o reset: If reset is WizardTrue, then the stopwatch is reset prior to
376% starting. If reset is WizardFalse, then timing is continued without
377% resetting the stopwatch.
378%
379*/
381 const WizardBooleanType reset)
382{
383 assert(timer_info != (TimerInfo *) NULL);
384 assert(timer_info->signature == WizardSignature);
385 if (reset != WizardFalse)
386 {
387 /*
388 Reset the stopwatch before starting it.
389 */
390 timer_info->user.total=0.0;
391 timer_info->elapsed.total=0.0;
392 }
393 if (timer_info->state != RunningTimerState)
394 {
395 timer_info->elapsed.start=ElapsedTime();
396 timer_info->user.start=UserTime();
397 }
398 timer_info->state=RunningTimerState;
399}
400
401/*
402%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
403% %
404% %
405% %
406+ S t o p T i m e r %
407% %
408% %
409% %
410%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
411%
412% StopTimer() stops the stopwatch.
413%
414% The format of the StopTimer method is:
415%
416% void StopTimer(TimerInfo *timer_info)
417%
418% A description of each parameter follows.
419%
420% o timer_info: Timer statistics structure.
421%
422*/
423static void StopTimer(TimerInfo *timer_info)
424{
425 assert(timer_info != (TimerInfo *) NULL);
426 assert(timer_info->signature == WizardSignature);
427 timer_info->elapsed.stop=ElapsedTime();
428 timer_info->user.stop=UserTime();
429 if (timer_info->state == RunningTimerState)
430 {
431 timer_info->user.total+=timer_info->user.stop-
432 timer_info->user.start+WizardEpsilon;
433 timer_info->elapsed.total+=timer_info->elapsed.stop-
434 timer_info->elapsed.start+WizardEpsilon;
435 }
436 timer_info->state=StoppedTimerState;
437}
438
439/*
440%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
441% %
442% %
443% %
444+ U s e r T i m e %
445% %
446% %
447% %
448%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
449%
450% UserTime() returns the total time the process has been scheduled (in
451% seconds) since the last call to StartTimer().
452%
453% The format of the UserTime method is:
454%
455% double UserTime()
456%
457*/
458static double UserTime(void)
459{
460#if defined(WIZARDSTOOLKIT_HAVE_TIMES) && defined(WIZARDSTOOLKIT_HAVE_SYSCONF)
461 struct tms
462 timer;
463
464 (void) times(&timer);
465 return((double) (timer.tms_utime+timer.tms_stime)/sysconf(_SC_CLK_TCK));
466#else
467#if defined(WIZARDSTOOLKIT_WINDOWS_SUPPORT)
468 return(NTUserTime());
469#else
470 return((double) clock()/CLOCKS_PER_SEC);
471#endif
472#endif
473}
#define WizardAssert(domain, predicate)
#define ThrowWizardFatalError(domain, error)
@ CipherDomain
Definition exception.h:34
@ MemoryError
Definition exception.h:49
WizardExport void * AcquireWizardMemory(const size_t size)
Definition memory.c:586
WizardExport void * RelinquishWizardMemory(void *memory)
Definition memory.c:1039
#define WizardExport
#define WizardSignature
Timer user
Definition timer.c:56
size_t signature
Definition timer.c:63
TimerState state
Definition timer.c:60
Timer elapsed
Definition timer.c:57
Definition timer.h:35
double stop
Definition timer.h:38
double total
Definition timer.h:39
double start
Definition timer.h:37
WizardExport double GetUserTime(TimerInfo *timer_info)
Definition timer.c:312
static double UserTime(void)
Definition timer.c:458
WizardExport WizardBooleanType ContinueTimer(TimerInfo *timer_info)
Definition timer.c:138
static double ElapsedTime(void)
Definition timer.c:204
WizardExport void StartTimer(TimerInfo *timer_info, const WizardBooleanType reset)
Definition timer.c:380
static void StopTimer(TimerInfo *)
Definition timer.c:423
#define CLOCKS_PER_SEC
Definition timer.c:70
WizardExport TimerInfo * DestroyTimerInfo(TimerInfo *timer_info)
Definition timer.c:176
WizardExport double GetElapsedTime(TimerInfo *timer_info)
Definition timer.c:244
WizardExport void ResetTimer(TimerInfo *timer_info)
Definition timer.c:345
WizardExport TimerInfo * AcquireTimerInfo(void)
Definition timer.c:101
WizardExport void GetTimerInfo(TimerInfo *timer_info)
Definition timer.c:278
#define WizardEpsilon
Definition timer.h:25
TimerState
Definition timer.h:28
@ RunningTimerState
Definition timer.h:31
@ StoppedTimerState
Definition timer.h:30
@ UndefinedTimerState
Definition timer.h:29
WizardBooleanType
Definition wizard-type.h:26
@ WizardTrue
Definition wizard-type.h:28
@ WizardFalse
Definition wizard-type.h:27