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
RevLine 
[219]1//
[3636]2// $Id: ctimer.cc,v 1.9 2009-05-26 20:24:23 ansari Exp $
[219]3//
4
[3579]5#include "machdefs.h"
[219]6#include "ctimer.h"
7
[3579]8#include <iostream>
9#include <stdio.h>
10#include <string>
11
[219]12//++
13// Class Timer
[895]14// Lib SysTools
[219]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
[895]32/*!
33 \class SOPHYA::Timer
[913]34 \ingroup SysTools
[2598]35 \brief Simple chronometer for CPU and elapsed time measurements.
[895]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.
[3274]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.
[895]42 displays the total CPU and elapsed time since timer creation.
43 The macro \b TIMEF create a timer object with the function name.
[3274]44 The elapsed and CPU time are displayed at the end of the bloc or function.
[895]45*/
46
[3636]47namespace SOPHYA {
[3274]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)
[219]56{
57 cpu0 = cpuSplit = clock();
[3579]58 gettimeofday(&elapse0, NULL);
59 elapseSplit = elapse0;
60// elapse0 = elapseSplit = time(0);
[3274]61 cpuSecT = cpuSecP = 0.;
62 elapSecT = elapSecP = 0;
[219]63}
64
[3274]65//! The destructor call Split() if the object has been created with prtflag=true.
66Timer::~Timer()
67{
68 if (defprtflg) Split();
69}
[219]70
71
[3274]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
[895]75 An optional message can be passed to be used instead of the
76 timer name
77*/
[3636]78void Timer::SplitQ()
[219]79{
[3579]80 struct timeval elapse;
81 gettimeofday(&elapse, NULL);
82
[219]83 clock_t cpu = clock();
84
[3579]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;
[219]91
[3579]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;
[219]99
[3274]100
101 // cumlcpu += (double)cpuSecP;
102 // cumulelapse += (int_8)etm;
103
104 elapseSplit = elapse;
105 cpuSplit = cpu;
[3636]106}
[3274]107
[3636]108void Timer::Split(const char* comm, bool prfg)
109{
110 SplitQ();
[3274]111 if ( !defprtflg && !prfg ) return;
[895]112 cout << "***Timing " << (comm ? comm : timerName.c_str()) << endl;
[3636]113 Print(cout);
114}
[219]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
[3636]120
121ostream& Timer::Print(ostream& os) const
122{
123
[3579]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",
[219]129 cpuSecT, cpuSecP);
[3636]130 os << out << endl;
[219]131
[3579]132 if (etmt<60) {
133 int_4 etmtms = elapSecT%1000;
[3636]134 sprintf(out,"%02d.%03d s", etmt, etmtms);
[3579]135 }
136 else
137 sprintf(out,"%02d:%02d:%02d", etmt/3600, (etmt%3600)/60, etmt%60);
[219]138
[3579]139 if (etm<60) {
140 int_4 etmms = elapSecT%1000;
[3636]141 sprintf(outp,"%02d.%03d s", etm, etmms);
[3579]142 }
143 else
144 sprintf(outp,"%02d:%02d:%02d", etm/3600, (etm%3600)/60, etm%60);
145
[3636]146 os << "Elapsed Time: Total=" << out << " (Partial= " << outp << ")" << endl;
[219]147}
148
[3636]149} // FIN namespace SOPHYA
Note: See TracBrowser for help on using the repository browser.