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

Last change on this file since 3579 was 3579, checked in by ansari, 17 years ago

Amelioration classe Timer, precision time-elapsed par utilisation gettimeofday(...) au lieu de time(), amelioration calcul temps cpu pour jobs tres long - Reza 20/02/2009

File size: 3.9 KB
Line 
1//
2// $Id: ctimer.cc,v 1.8 2009-02-20 11:07:02 ansari Exp $
3//
4
5#include "machdefs.h"
6#include "sopnamsp.h"
7#include "ctimer.h"
8
9#include <iostream>
10#include <stdio.h>
11#include <string>
12
13//++
14// Class Timer
15// Lib SysTools
16// include ctimer.h
17//
18// Chronométrage de programmes. Le constructeur mémorise l'heure et
19// le temps CPU, ainsi qu'un message éventuel.
20//
21// Split affiche le temps partiel.
22//
23// Le destructeur affiche le temps total (CPU, et écoulé).
24//
25// Des macros permettent une utilisation simplifiée :
26// * TIMEF crée un objet de type Timer, avec le nom de la function
27// courante comme message. Le temps écoulé sera affiché à la sortie
28// de la function.
29//--
30
31
32
33/*!
34 \class SOPHYA::Timer
35 \ingroup SysTools
36 \brief Simple chronometer for CPU and elapsed time measurements.
37 This class implements a simple chronometer which can be used for
38 measuring the CPU and elapsed time in functions. The constructor
39 keeps the start time (and CPU time) and an optional message.
40 The \b Split updates partial times and optionaly displays partial and
41 total CPU and elapsed time.
42 The destructor calls Split() if the object has been created with prfg=true.
43 displays the total CPU and elapsed time since timer creation.
44 The macro \b TIMEF create a timer object with the function name.
45 The elapsed and CPU time are displayed at the end of the bloc or function.
46*/
47
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::Split(const char* comm, bool prfg)
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 if ( !defprtflg && !prfg ) return;
108
109 cout << "***Timing " << (comm ? comm : timerName.c_str()) << endl;
110
111// Pour des formats comme ca, la syntaxe printf est plus agreable.
112// Pour ne pas melanger stdio/iostream (pb de desynchronisation sur
113// autres C++ que GNU), on fait un cout << chaine.
114
115 int_4 etm = elapSecP/1000;
116 int_4 etmt = elapSecT/1000;
117
118 char out[200],outp[64];
119 sprintf(out,"CPU Time: Total= %lg (Partial= %lg) s",
120 cpuSecT, cpuSecP);
121 cout << out << endl;
122
123 if (etmt<60) {
124 int_4 etmtms = elapSecT%1000;
125 sprintf(out,"%02d.%03d ms", etmt, etmtms);
126 }
127 else
128 sprintf(out,"%02d:%02d:%02d", etmt/3600, (etmt%3600)/60, etmt%60);
129
130 if (etm<60) {
131 int_4 etmms = elapSecT%1000;
132 sprintf(outp,"%02d.%03d ms", etm, etmms);
133 }
134 else
135 sprintf(outp,"%02d:%02d:%02d", etm/3600, (etm%3600)/60, etm%60);
136
137 cout << "Elapsed Time: Total=" << out << " (Partial= " << outp << ")" << endl;
138}
139
Note: See TracBrowser for help on using the repository browser.