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

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

Correction bug plantage Timer(NULL) - Reza 26/05/2009

File size: 4.3 KB
Line 
1//
2// $Id: ctimer.cc,v 1.10 2009-05-26 21:39:54 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//! Default constructor with optional default print flag
49Timer::Timer(bool prfg)
50 : defprtflg(prfg)
51{
52 cpu0 = cpuSplit = clock();
53 gettimeofday(&elapse0, NULL);
54 elapseSplit = elapse0;
55// elapse0 = elapseSplit = time(0);
56 cpuSecT = cpuSecP = 0.;
57 elapSecT = elapSecP = 0;
58}
59/*!
60 \brief Constructor with the specification of a name or message
61 and optional default print flag.
62 if \b prfg==true , a call to Split() causes the display of partial and
63 total CPU and elapsed time.
64*/
65Timer::Timer(const char* name, bool prfg)
66 : timerName(((name!=NULL)?name:"")) , defprtflg(prfg)
67{
68 cpu0 = cpuSplit = clock();
69 gettimeofday(&elapse0, NULL);
70 elapseSplit = elapse0;
71// elapse0 = elapseSplit = time(0);
72 cpuSecT = cpuSecP = 0.;
73 elapSecT = elapSecP = 0;
74}
75
76//! The destructor call Split() if the object has been created with prtflag=true.
77Timer::~Timer()
78{
79 if (defprtflg) Split();
80}
81
82
83/*! This method updates the CPU and elapsed time.
84 If the prfg==true or the constructor prfg==true, it also displays (prints)
85 the partial CPU and elapsed time
86 An optional message can be passed to be used instead of the
87 timer name
88*/
89void Timer::SplitQ()
90{
91 struct timeval elapse;
92 gettimeofday(&elapse, NULL);
93
94 clock_t cpu = clock();
95
96 uint_8 deltacpu;
97 if (cpu < cpuSplit) {
98 clock_t cpumax = 0; cpumax = ~cpumax;
99 deltacpu = (uint_8)(cpumax-cpuSplit)+cpu;
100 }
101 else deltacpu = cpu - cpuSplit;
102
103 cpuSecT += ((double)deltacpu) / (double)(CLOCKS_PER_SEC);
104 cpuSecP = ((double)deltacpu) / (double)(CLOCKS_PER_SEC);
105
106 elapSecP = (elapse.tv_sec - elapseSplit.tv_sec)*1000
107 + (elapse.tv_usec - elapseSplit.tv_usec)/1000;
108 elapSecT = (elapse.tv_sec - elapse0.tv_sec)*1000
109 + (elapse.tv_usec - elapse0.tv_usec)/1000;
110
111
112 // cumlcpu += (double)cpuSecP;
113 // cumulelapse += (int_8)etm;
114
115 elapseSplit = elapse;
116 cpuSplit = cpu;
117}
118
119void Timer::Split(const char* comm, bool prfg)
120{
121 SplitQ();
122 if ( !defprtflg && !prfg ) return;
123 cout << "***Timing " << (comm ? comm : timerName.c_str()) << endl;
124 Print(cout);
125}
126
127// Pour des formats comme ca, la syntaxe printf est plus agreable.
128// Pour ne pas melanger stdio/iostream (pb de desynchronisation sur
129// autres C++ que GNU), on fait un cout << chaine.
130
131
132ostream& Timer::Print(ostream& os) const
133{
134
135 int_4 etm = elapSecP/1000;
136 int_4 etmt = elapSecT/1000;
137
138 char out[200],outp[64];
139 sprintf(out,"CPU Time: Total= %lg (Partial= %lg) s",
140 cpuSecT, cpuSecP);
141 os << out << endl;
142
143 if (etmt<60) {
144 int_4 etmtms = elapSecT%1000;
145 sprintf(out,"%02d.%03d s", etmt, etmtms);
146 }
147 else
148 sprintf(out,"%02d:%02d:%02d", etmt/3600, (etmt%3600)/60, etmt%60);
149
150 if (etm<60) {
151 int_4 etmms = elapSecT%1000;
152 sprintf(outp,"%02d.%03d s", etm, etmms);
153 }
154 else
155 sprintf(outp,"%02d:%02d:%02d", etm/3600, (etm%3600)/60, etm%60);
156
157 os << "Elapsed Time: Total=" << out << " (Partial= " << outp << ")" << endl;
158}
159
160} // FIN namespace SOPHYA
Note: See TracBrowser for help on using the repository browser.