1 | //
|
---|
2 | // $Id: ctimer.cc,v 1.7 2007-07-06 12:54:12 ansari Exp $
|
---|
3 | //
|
---|
4 |
|
---|
5 | #include "sopnamsp.h"
|
---|
6 | #include "machdefs.h"
|
---|
7 | #include "ctimer.h"
|
---|
8 |
|
---|
9 | //++
|
---|
10 | // Class Timer
|
---|
11 | // Lib SysTools
|
---|
12 | // include ctimer.h
|
---|
13 | //
|
---|
14 | // Chronométrage de programmes. Le constructeur mémorise l'heure et
|
---|
15 | // le temps CPU, ainsi qu'un message éventuel.
|
---|
16 | //
|
---|
17 | // Split affiche le temps partiel.
|
---|
18 | //
|
---|
19 | // Le destructeur affiche le temps total (CPU, et écoulé).
|
---|
20 | //
|
---|
21 | // Des macros permettent une utilisation simplifiée :
|
---|
22 | // * TIMEF crée un objet de type Timer, avec le nom de la function
|
---|
23 | // courante comme message. Le temps écoulé sera affiché à la sortie
|
---|
24 | // de la function.
|
---|
25 | //--
|
---|
26 |
|
---|
27 |
|
---|
28 |
|
---|
29 | /*!
|
---|
30 | \class SOPHYA::Timer
|
---|
31 | \ingroup SysTools
|
---|
32 | \brief Simple chronometer for CPU and elapsed time measurements.
|
---|
33 | This class implements a simple chronometer which can be used for
|
---|
34 | measuring the CPU and elapsed time in functions. The constructor
|
---|
35 | keeps the start time (and CPU time) and an optional message.
|
---|
36 | The \b Split updates partial times and optionaly displays partial and
|
---|
37 | total CPU and elapsed time.
|
---|
38 | The destructor calls Split() if the object has been created with prfg=true.
|
---|
39 | displays the total CPU and elapsed time since timer creation.
|
---|
40 | The macro \b TIMEF create a timer object with the function name.
|
---|
41 | The elapsed and CPU time are displayed at the end of the bloc or function.
|
---|
42 | */
|
---|
43 |
|
---|
44 | /*!
|
---|
45 | \brief Constructor with the specification of a optional name or message
|
---|
46 | and default print flag.
|
---|
47 | if \b prfg==true , a call to Split() causes the display of partial and
|
---|
48 | total CPU and elapsed time.
|
---|
49 | */
|
---|
50 | Timer::Timer(const char* name, bool prfg)
|
---|
51 | : timerName(name) , defprtflg(prfg)
|
---|
52 | {
|
---|
53 | cpu0 = cpuSplit = clock();
|
---|
54 | elapse0 = elapseSplit = time(0);
|
---|
55 | cpuSecT = cpuSecP = 0.;
|
---|
56 | elapSecT = elapSecP = 0;
|
---|
57 | }
|
---|
58 |
|
---|
59 | //! The destructor call Split() if the object has been created with prtflag=true.
|
---|
60 | Timer::~Timer()
|
---|
61 | {
|
---|
62 | if (defprtflg) Split();
|
---|
63 | }
|
---|
64 |
|
---|
65 |
|
---|
66 | /*! This method updates the CPU and elapsed time.
|
---|
67 | If the prfg==true or the constructor prfg==true, it also displays (prints)
|
---|
68 | the partial CPU and elapsed time
|
---|
69 | An optional message can be passed to be used instead of the
|
---|
70 | timer name
|
---|
71 | */
|
---|
72 | void Timer::Split(const char* comm, bool prfg)
|
---|
73 | {
|
---|
74 | time_t elapse = time(0);
|
---|
75 | clock_t cpu = clock();
|
---|
76 |
|
---|
77 | cpuSecT = ((float)cpu - (float)cpu0) / (float)(CLOCKS_PER_SEC);
|
---|
78 | cpuSecP = ((float)cpu - (float)cpuSplit) / (float)(CLOCKS_PER_SEC);
|
---|
79 |
|
---|
80 | elapSecP = elapse - elapseSplit;
|
---|
81 | elapSecT = elapse - elapse0;
|
---|
82 |
|
---|
83 |
|
---|
84 | // cumlcpu += (double)cpuSecP;
|
---|
85 | // cumulelapse += (int_8)etm;
|
---|
86 |
|
---|
87 | elapseSplit = elapse;
|
---|
88 | cpuSplit = cpu;
|
---|
89 |
|
---|
90 | if ( !defprtflg && !prfg ) return;
|
---|
91 |
|
---|
92 | cout << "***Timing " << (comm ? comm : timerName.c_str()) << endl;
|
---|
93 |
|
---|
94 | // Pour des formats comme ca, la syntaxe printf est plus agreable.
|
---|
95 | // Pour ne pas melanger stdio/iostream (pb de desynchronisation sur
|
---|
96 | // autres C++ que GNU), on fait un cout << chaine.
|
---|
97 |
|
---|
98 | int etm = elapSecP;
|
---|
99 | int etmt = elapSecT;
|
---|
100 | char out[200];
|
---|
101 | sprintf(out,"CPU Time: Total= %g (Partial= %g) Sec.",
|
---|
102 | cpuSecT, cpuSecP);
|
---|
103 | cout << out << endl;
|
---|
104 |
|
---|
105 | sprintf(out,"Elapsed Time: Total= %02d:%02d:%02d (Partial=%02d:%02d:%02d)",
|
---|
106 | etmt/3600, (etmt%3600)/60, etmt%60,
|
---|
107 | etm/3600, (etm%3600)/60, etm%60);
|
---|
108 |
|
---|
109 | cout << out << endl;
|
---|
110 | }
|
---|
111 |
|
---|