1 | // This may look like C code, but it is really -*- C++ -*-
|
---|
2 | //
|
---|
3 | // $Id: ctimer.h,v 1.7 2009-05-26 20:24:24 ansari Exp $
|
---|
4 | //
|
---|
5 |
|
---|
6 |
|
---|
7 | #ifndef CTIMER_SEEN
|
---|
8 | #define CTIMER_SEEN
|
---|
9 |
|
---|
10 | #include "machdefs.h"
|
---|
11 | #include <sys/types.h>
|
---|
12 | #include <time.h>
|
---|
13 | #include <sys/time.h>
|
---|
14 | #include <string>
|
---|
15 | #include <iostream>
|
---|
16 |
|
---|
17 | using namespace std;
|
---|
18 |
|
---|
19 | // <summary> Permet de chronometrer des fonctions. </summary>
|
---|
20 | // Class Timer qui memorise l'heure de sa creation, et le temps CPU.
|
---|
21 | // A la fin du bloc ou de la procedure, l'objet est detruit,
|
---|
22 | // et son destructeur affiche le temps ecoule.
|
---|
23 |
|
---|
24 | namespace SOPHYA {
|
---|
25 |
|
---|
26 | //! Simple chronometer class
|
---|
27 | class Timer {
|
---|
28 | public:
|
---|
29 | // L'objet memorise le temps CPU et l'heure, et le nom donne
|
---|
30 | Timer(const char* name=0, bool prfg=true);
|
---|
31 |
|
---|
32 | // Le destructeur appelle split sans parametre.
|
---|
33 | virtual ~Timer();
|
---|
34 |
|
---|
35 | // Affiche le temps ecoule total/partiel, avec le nom eventuel.
|
---|
36 | // Si pas de parametre affiche le nom donne a la creation.
|
---|
37 | void Split(const char* comm=0, bool prfg=false);
|
---|
38 |
|
---|
39 | //! store partial CPU and elapsed time (no print)
|
---|
40 | void SplitQ();
|
---|
41 |
|
---|
42 | // Sert a eviter que GNU ne pretende qu'on utilise pas l'objet...
|
---|
43 | /*! To avoid not used object compiler warnings */
|
---|
44 | void Nop() {}
|
---|
45 |
|
---|
46 | /*! \brief Return the total elapsed time (number of seconds),
|
---|
47 | between the object creation and the last call to Split().
|
---|
48 | */
|
---|
49 | inline int_8 TotalElapsedTime() { return elapSecT/1000 ; }
|
---|
50 | /*! \brief Return the partial elapsed time (number of seconds).
|
---|
51 | between the last two calls to Split().
|
---|
52 | */
|
---|
53 | inline int_8 PartialElapsedTime() { return elapSecP/1000 ; }
|
---|
54 |
|
---|
55 | /*! \brief Return the total elapsed time (in milli-seconds),
|
---|
56 | between the object creation and the last call to Split().
|
---|
57 | */
|
---|
58 | inline int_8 TotalElapsedTimems() { return elapSecT ; }
|
---|
59 | /*! \brief Return the partial elapsed time (in milli-seconds).
|
---|
60 | between the last two calls to Split().
|
---|
61 | */
|
---|
62 | inline int_8 PartialElapsedTimems() { return elapSecP ; }
|
---|
63 |
|
---|
64 | /*! \brief Return the total CPU time in seconds,
|
---|
65 | between the object creation and the last call to Split().
|
---|
66 | */
|
---|
67 | inline double TotalCPUTime() { return cpuSecT; }
|
---|
68 | /*! \brief Return the partial CPU time in seconds,
|
---|
69 | between the last two calls to Split().
|
---|
70 | */
|
---|
71 | inline double PartialCPUTime() { return cpuSecP; }
|
---|
72 |
|
---|
73 | //! Print the CPU and elapsed time on os (partial time corresponding to the last call to Split())
|
---|
74 | ostream& Print(ostream& os) const ;
|
---|
75 | //! Print the CPU and elapsed time on cout
|
---|
76 | inline ostream& Print() const { return Print(cout); }
|
---|
77 |
|
---|
78 | private:
|
---|
79 | clock_t cpu0, cpuSplit;
|
---|
80 | struct timeval elapse0, elapseSplit;
|
---|
81 | double cpuSecT, cpuSecP; // Total and partial CPU time
|
---|
82 | int_8 elapSecT, elapSecP; // Total and partial elapsed time in ms
|
---|
83 | string timerName;
|
---|
84 | bool defprtflg;
|
---|
85 | };
|
---|
86 |
|
---|
87 | /*! operator << overloading - Calls SplitQ() and Print() */
|
---|
88 | inline ostream& operator << (ostream& s, Timer& tm)
|
---|
89 | { tm.SplitQ(); tm.Print(s); return(s); }
|
---|
90 |
|
---|
91 | } // namespace SOPHYA
|
---|
92 |
|
---|
93 | #define TIMEF Timer timer(__PRETTY_FUNCTION__); timer.Nop();
|
---|
94 |
|
---|
95 | #endif // CTIMER_SEEN
|
---|