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