| 1 | //
 | 
|---|
| 2 | // $Id: ctimer.cc,v 1.6 2004-09-10 09:54:57 cmv 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 | //      * SPLITTIME affiche le temps partiel du compteur crée par TIMEF
 | 
|---|
| 26 | //      * TIMEN(nom) permet de donner un autre nom que le nom de la fonction.
 | 
|---|
| 27 | //--
 | 
|---|
| 28 | 
 | 
|---|
| 29 | 
 | 
|---|
| 30 | //++
 | 
|---|
| 31 | // Titre        Constructeurs
 | 
|---|
| 32 | //--
 | 
|---|
| 33 | 
 | 
|---|
| 34 | //++
 | 
|---|
| 35 | // Timer::Timer(const char* name)
 | 
|---|
| 36 | //      Crée un objet qui mémorise l'heure et le temps CPU du process courant.
 | 
|---|
| 37 | //      Le destructeur affiche les temps écoulé et CPU utilisé.
 | 
|---|
| 38 | //--
 | 
|---|
| 39 | 
 | 
|---|
| 40 | /*!
 | 
|---|
| 41 |   \class SOPHYA::Timer
 | 
|---|
| 42 |   \ingroup SysTools
 | 
|---|
| 43 |   \brief Simple chronometer for CPU and elapsed time measurements.
 | 
|---|
| 44 |   This class implements a simple chronometer which can be used for
 | 
|---|
| 45 |   measuring the CPU and elapsed time in functions. The constructor
 | 
|---|
| 46 |   keeps the start time (and CPU time) and an optional message. 
 | 
|---|
| 47 |   The \b Split method displays the partial time, and destructor 
 | 
|---|
| 48 |   displays the total CPU and elapsed time since timer creation.
 | 
|---|
| 49 |   The macro \b  TIMEF create a timer object with the function name.
 | 
|---|
| 50 |   The macro \b  SPLITTIME calls the split methode for the timer created
 | 
|---|
| 51 |   by TIMEF. A named timer can be created using the macro \b TIMEN(nom)
 | 
|---|
| 52 | */
 | 
|---|
| 53 | 
 | 
|---|
| 54 | /*! Constructor with the specification of a optional name or message */
 | 
|---|
| 55 | Timer::Timer(const char* name)
 | 
|---|
| 56 | : timerName(name)
 | 
|---|
| 57 | {
 | 
|---|
| 58 |   cpu0 = cpuSplit = clock();
 | 
|---|
| 59 |   elapse0 = elapseSplit = time(0);
 | 
|---|
| 60 | }
 | 
|---|
| 61 | 
 | 
|---|
| 62 | //++
 | 
|---|
| 63 | // Titre        Méthodes
 | 
|---|
| 64 | //--
 | 
|---|
| 65 | 
 | 
|---|
| 66 | //++
 | 
|---|
| 67 | // Timer::Split(const char* comm)
 | 
|---|
| 68 | //      Affiche le temps partiel.
 | 
|---|
| 69 | //--
 | 
|---|
| 70 | 
 | 
|---|
| 71 | /*! Method which displays the partial CPU and elapsed time
 | 
|---|
| 72 |     An optional message can be passed to be used instead of the
 | 
|---|
| 73 |     timer name
 | 
|---|
| 74 | */
 | 
|---|
| 75 | void Timer::Split(const char* comm)
 | 
|---|
| 76 | {
 | 
|---|
| 77 |   time_t elapse = time(0);
 | 
|---|
| 78 |   clock_t cpu   = clock();
 | 
|---|
| 79 | 
 | 
|---|
| 80 |   float cpuSecT = ((float)cpu - (float)cpu0) / (float)(CLOCKS_PER_SEC);
 | 
|---|
| 81 |   float cpuSecP = ((float)cpu - (float)cpuSplit) / (float)(CLOCKS_PER_SEC);
 | 
|---|
| 82 |   
 | 
|---|
| 83 |   int etm  = elapse - elapseSplit;
 | 
|---|
| 84 |   int etmt = elapse - elapse0;
 | 
|---|
| 85 | 
 | 
|---|
| 86 |   cout << "***Timing " << (comm ? comm : timerName.c_str()) << endl;
 | 
|---|
| 87 | 
 | 
|---|
| 88 | // Pour des formats comme ca, la syntaxe printf est plus agreable.
 | 
|---|
| 89 | // Pour ne pas melanger stdio/iostream (pb de desynchronisation sur
 | 
|---|
| 90 | // autres C++ que GNU), on fait un cout << chaine.
 | 
|---|
| 91 | 
 | 
|---|
| 92 |   char out[200];
 | 
|---|
| 93 |   sprintf(out,"CPU     Time: Total= %g (Partial= %g) Sec.",
 | 
|---|
| 94 |           cpuSecT, cpuSecP);
 | 
|---|
| 95 |   cout << out << endl;
 | 
|---|
| 96 | 
 | 
|---|
| 97 |   sprintf(out,"Elapsed Time: Total= %02d:%02d:%02d (Partial=%02d:%02d:%02d)",
 | 
|---|
| 98 |           etmt/3600, (etmt%3600)/60, etmt%60, 
 | 
|---|
| 99 |           etm/3600,  (etm%3600)/60,  etm%60);
 | 
|---|
| 100 | 
 | 
|---|
| 101 |   cout << out << endl;
 | 
|---|
| 102 | 
 | 
|---|
| 103 |   elapseSplit = elapse;
 | 
|---|
| 104 |   cpuSplit    = cpu;
 | 
|---|
| 105 | }
 | 
|---|
| 106 | 
 | 
|---|
| 107 | Timer::~Timer()
 | 
|---|
| 108 | {
 | 
|---|
| 109 |   Split();
 | 
|---|
| 110 | }
 | 
|---|