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