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

Last change on this file since 3636 was 3636, checked in by ansari, 16 years ago

Petites correction/amelioration de la classe Timer - Reza 26/05/2009

File size: 4.0 KB
Line 
1//
2// $Id: ctimer.cc,v 1.9 2009-05-26 20:24:23 ansari Exp $
3//
4
5#include "machdefs.h"
6#include "ctimer.h"
7
8#include <iostream>
9#include <stdio.h>
10#include <string>
11
12//++
13// Class Timer
14// Lib SysTools
15// include ctimer.h
16//
17// Chronométrage de programmes. Le constructeur mémorise l'heure et
18// le temps CPU, ainsi qu'un message éventuel.
19//
20// Split affiche le temps partiel.
21//
22// Le destructeur affiche le temps total (CPU, et écoulé).
23//
24// Des macros permettent une utilisation simplifiée :
25// * TIMEF crée un objet de type Timer, avec le nom de la function
26// courante comme message. Le temps écoulé sera affiché à la sortie
27// de la function.
28//--
29
30
31
32/*!
33 \class SOPHYA::Timer
34 \ingroup SysTools
35 \brief Simple chronometer for CPU and elapsed time measurements.
36 This class implements a simple chronometer which can be used for
37 measuring the CPU and elapsed time in functions. The constructor
38 keeps the start time (and CPU time) and an optional message.
39 The \b Split updates partial times and optionaly displays partial and
40 total CPU and elapsed time.
41 The destructor calls Split() if the object has been created with prfg=true.
42 displays the total CPU and elapsed time since timer creation.
43 The macro \b TIMEF create a timer object with the function name.
44 The elapsed and CPU time are displayed at the end of the bloc or function.
45*/
46
47namespace SOPHYA {
48/*!
49 \brief Constructor with the specification of a optional name or message
50 and default print flag.
51 if \b prfg==true , a call to Split() causes the display of partial and
52 total CPU and elapsed time.
53*/
54Timer::Timer(const char* name, bool prfg)
55 : timerName(name) , defprtflg(prfg)
56{
57 cpu0 = cpuSplit = clock();
58 gettimeofday(&elapse0, NULL);
59 elapseSplit = elapse0;
60// elapse0 = elapseSplit = time(0);
61 cpuSecT = cpuSecP = 0.;
62 elapSecT = elapSecP = 0;
63}
64
65//! The destructor call Split() if the object has been created with prtflag=true.
66Timer::~Timer()
67{
68 if (defprtflg) Split();
69}
70
71
72/*! This method updates the CPU and elapsed time.
73 If the prfg==true or the constructor prfg==true, it also displays (prints)
74 the partial CPU and elapsed time
75 An optional message can be passed to be used instead of the
76 timer name
77*/
78void Timer::SplitQ()
79{
80 struct timeval elapse;
81 gettimeofday(&elapse, NULL);
82
83 clock_t cpu = clock();
84
85 uint_8 deltacpu;
86 if (cpu < cpuSplit) {
87 clock_t cpumax = 0; cpumax = ~cpumax;
88 deltacpu = (uint_8)(cpumax-cpuSplit)+cpu;
89 }
90 else deltacpu = cpu - cpuSplit;
91
92 cpuSecT += ((double)deltacpu) / (double)(CLOCKS_PER_SEC);
93 cpuSecP = ((double)deltacpu) / (double)(CLOCKS_PER_SEC);
94
95 elapSecP = (elapse.tv_sec - elapseSplit.tv_sec)*1000
96 + (elapse.tv_usec - elapseSplit.tv_usec)/1000;
97 elapSecT = (elapse.tv_sec - elapse0.tv_sec)*1000
98 + (elapse.tv_usec - elapse0.tv_usec)/1000;
99
100
101 // cumlcpu += (double)cpuSecP;
102 // cumulelapse += (int_8)etm;
103
104 elapseSplit = elapse;
105 cpuSplit = cpu;
106}
107
108void Timer::Split(const char* comm, bool prfg)
109{
110 SplitQ();
111 if ( !defprtflg && !prfg ) return;
112 cout << "***Timing " << (comm ? comm : timerName.c_str()) << endl;
113 Print(cout);
114}
115
116// Pour des formats comme ca, la syntaxe printf est plus agreable.
117// Pour ne pas melanger stdio/iostream (pb de desynchronisation sur
118// autres C++ que GNU), on fait un cout << chaine.
119
120
121ostream& Timer::Print(ostream& os) const
122{
123
124 int_4 etm = elapSecP/1000;
125 int_4 etmt = elapSecT/1000;
126
127 char out[200],outp[64];
128 sprintf(out,"CPU Time: Total= %lg (Partial= %lg) s",
129 cpuSecT, cpuSecP);
130 os << out << endl;
131
132 if (etmt<60) {
133 int_4 etmtms = elapSecT%1000;
134 sprintf(out,"%02d.%03d s", etmt, etmtms);
135 }
136 else
137 sprintf(out,"%02d:%02d:%02d", etmt/3600, (etmt%3600)/60, etmt%60);
138
139 if (etm<60) {
140 int_4 etmms = elapSecT%1000;
141 sprintf(outp,"%02d.%03d s", etm, etmms);
142 }
143 else
144 sprintf(outp,"%02d:%02d:%02d", etm/3600, (etm%3600)/60, etm%60);
145
146 os << "Elapsed Time: Total=" << out << " (Partial= " << outp << ")" << endl;
147}
148
149} // FIN namespace SOPHYA
Note: See TracBrowser for help on using the repository browser.