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