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

Last change on this file since 911 was 895, checked in by ansari, 25 years ago

Documentation de fichiers - Reza 12/4/2000

File size: 2.9 KB
Line 
1//
2// $Id: ctimer.cc,v 1.3 2000-04-12 17:49:40 ansari Exp $
3//
4
5#include "machdefs.h"
6#include "ctimer.h"
7
8//++
9// Class Timer
10// Lib SysTools
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
39/*!
40 \class SOPHYA::Timer
41 This class implements a simple chronometer which can be used for
42 measuring the CPU and elapsed time in functions. The constructor
43 keeps the start time (and CPU time) and an optional message.
44 The \b Split method displays the partial time, and destructor
45 displays the total CPU and elapsed time since timer creation.
46 The macro \b TIMEF create a timer object with the function name.
47 The macro \b SPLITTIME calls the split methode for the timer created
48 by TIMEF. A named timer can be created using the macro \b TIMEN(nom)
49*/
50
51/*! Constructor with the specification of a optional name or message */
52Timer::Timer(const char* name)
53: timerName(name)
54{
55 cpu0 = cpuSplit = clock();
56 elapse0 = elapseSplit = time(0);
57}
58
59//++
60// Titre Méthodes
61//--
62
63//++
64// Timer::Split(const char* comm)
65// Affiche le temps partiel.
66//--
67
68/*! Method which displays the partial CPU and elapsed time
69 An optional message can be passed to be used instead of the
70 timer name
71*/
72void Timer::Split(const char* comm)
73{
74 time_t elapse = time(0);
75 clock_t cpu = clock();
76
77 float cpuSecT = ((float)cpu - (float)cpu0) / (float)(CLOCKS_PER_SEC);
78 float cpuSecP = ((float)cpu - (float)cpuSplit) / (float)(CLOCKS_PER_SEC);
79
80 int etm = elapse - elapseSplit;
81 int etmt = elapse - elapse0;
82
83 cout << "***Timing " << (comm ? comm : timerName.c_str()) << endl;
84
85// Pour des formats comme ca, la syntaxe printf est plus agreable.
86// Pour ne pas melanger stdio/iostream (pb de desynchronisation sur
87// autres C++ que GNU), on fait un cout << chaine.
88
89 char out[200];
90 sprintf(out,"CPU Time: Total= %g (Partial= %g) Sec.",
91 cpuSecT, cpuSecP);
92 cout << out << endl;
93
94 sprintf(out,"Elapsed Time: Total= %02d:%02d:%02d (Partial=%02d:%02d:%02d)",
95 etmt/3600, (etmt%3600)/60, etmt%60,
96 etm/3600, (etm%3600)/60, etm%60);
97
98 cout << out << endl;
99
100 elapseSplit = elapse;
101 cpuSplit = cpu;
102}
103
104Timer::~Timer()
105{
106 Split();
107}
Note: See TracBrowser for help on using the repository browser.