source: Sophya/trunk/SophyaLib/SysTools/ctimer.cc@ 2599

Last change on this file since 2599 was 2598, checked in by ansari, 21 years ago

Documentation (ajoutee ou completee) pour les classes du module SysTools - Reza 11 Aout 2004

File size: 3.0 KB
RevLine 
[219]1//
[2598]2// $Id: ctimer.cc,v 1.5 2004-08-11 13:10:25 ansari Exp $
[219]3//
4
[241]5#include "machdefs.h"
[219]6#include "ctimer.h"
7
8//++
9// Class Timer
[895]10// Lib SysTools
[219]11// include ctimer.h
12//
13// Chronométrage de programmes. Le constructeur mémorise l'heure et
14// le temps CPU, ainsi qu'un message éventuel.
15//
16// Split affiche le temps partiel.
17//
18// Le destructeur affiche le temps total (CPU, et écoulé).
19//
20// Des macros permettent une utilisation simplifiée :
21// * TIMEF crée un objet de type Timer, avec le nom de la function
22// courante comme message. Le temps écoulé sera affiché à la sortie
23// de la function.
24// * SPLITTIME affiche le temps partiel du compteur crée par TIMEF
25// * TIMEN(nom) permet de donner un autre nom que le nom de la fonction.
26//--
27
28
29//++
30// Titre Constructeurs
31//--
32
33//++
34// Timer::Timer(const char* name)
35// Crée un objet qui mémorise l'heure et le temps CPU du process courant.
36// Le destructeur affiche les temps écoulé et CPU utilisé.
37//--
38
[895]39/*!
40 \class SOPHYA::Timer
[913]41 \ingroup SysTools
[2598]42 \brief Simple chronometer for CPU and elapsed time measurements.
[895]43 This class implements a simple chronometer which can be used for
44 measuring the CPU and elapsed time in functions. The constructor
45 keeps the start time (and CPU time) and an optional message.
46 The \b Split method displays the partial time, and destructor
47 displays the total CPU and elapsed time since timer creation.
48 The macro \b TIMEF create a timer object with the function name.
49 The macro \b SPLITTIME calls the split methode for the timer created
50 by TIMEF. A named timer can be created using the macro \b TIMEN(nom)
51*/
52
53/*! Constructor with the specification of a optional name or message */
[219]54Timer::Timer(const char* name)
55: timerName(name)
56{
57 cpu0 = cpuSplit = clock();
58 elapse0 = elapseSplit = time(0);
59}
60
61//++
62// Titre Méthodes
63//--
64
65//++
66// Timer::Split(const char* comm)
67// Affiche le temps partiel.
68//--
69
[895]70/*! Method which displays the partial CPU and elapsed time
71 An optional message can be passed to be used instead of the
72 timer name
73*/
[219]74void Timer::Split(const char* comm)
75{
76 time_t elapse = time(0);
77 clock_t cpu = clock();
78
79 float cpuSecT = ((float)cpu - (float)cpu0) / (float)(CLOCKS_PER_SEC);
80 float cpuSecP = ((float)cpu - (float)cpuSplit) / (float)(CLOCKS_PER_SEC);
81
82 int etm = elapse - elapseSplit;
83 int etmt = elapse - elapse0;
84
[895]85 cout << "***Timing " << (comm ? comm : timerName.c_str()) << endl;
[219]86
87// Pour des formats comme ca, la syntaxe printf est plus agreable.
88// Pour ne pas melanger stdio/iostream (pb de desynchronisation sur
89// autres C++ que GNU), on fait un cout << chaine.
90
91 char out[200];
92 sprintf(out,"CPU Time: Total= %g (Partial= %g) Sec.",
93 cpuSecT, cpuSecP);
94 cout << out << endl;
95
96 sprintf(out,"Elapsed Time: Total= %02d:%02d:%02d (Partial=%02d:%02d:%02d)",
97 etmt/3600, (etmt%3600)/60, etmt%60,
98 etm/3600, (etm%3600)/60, etm%60);
99
100 cout << out << endl;
101
102 elapseSplit = elapse;
103 cpuSplit = cpu;
104}
105
106Timer::~Timer()
107{
108 Split();
109}
Note: See TracBrowser for help on using the repository browser.