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