[219] | 1 | //
|
---|
[3274] | 2 | // $Id: ctimer.cc,v 1.7 2007-07-06 12:54:12 ansari Exp $
|
---|
[219] | 3 | //
|
---|
| 4 |
|
---|
[2615] | 5 | #include "sopnamsp.h"
|
---|
[241] | 6 | #include "machdefs.h"
|
---|
[219] | 7 | #include "ctimer.h"
|
---|
| 8 |
|
---|
| 9 | //++
|
---|
| 10 | // Class Timer
|
---|
[895] | 11 | // Lib SysTools
|
---|
[219] | 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 | //--
|
---|
| 26 |
|
---|
| 27 |
|
---|
| 28 |
|
---|
[895] | 29 | /*!
|
---|
| 30 | \class SOPHYA::Timer
|
---|
[913] | 31 | \ingroup SysTools
|
---|
[2598] | 32 | \brief Simple chronometer for CPU and elapsed time measurements.
|
---|
[895] | 33 | This class implements a simple chronometer which can be used for
|
---|
| 34 | measuring the CPU and elapsed time in functions. The constructor
|
---|
| 35 | keeps the start time (and CPU time) and an optional message.
|
---|
[3274] | 36 | The \b Split updates partial times and optionaly displays partial and
|
---|
| 37 | total CPU and elapsed time.
|
---|
| 38 | The destructor calls Split() if the object has been created with prfg=true.
|
---|
[895] | 39 | displays the total CPU and elapsed time since timer creation.
|
---|
| 40 | The macro \b TIMEF create a timer object with the function name.
|
---|
[3274] | 41 | The elapsed and CPU time are displayed at the end of the bloc or function.
|
---|
[895] | 42 | */
|
---|
| 43 |
|
---|
[3274] | 44 | /*!
|
---|
| 45 | \brief Constructor with the specification of a optional name or message
|
---|
| 46 | and default print flag.
|
---|
| 47 | if \b prfg==true , a call to Split() causes the display of partial and
|
---|
| 48 | total CPU and elapsed time.
|
---|
| 49 | */
|
---|
| 50 | Timer::Timer(const char* name, bool prfg)
|
---|
| 51 | : timerName(name) , defprtflg(prfg)
|
---|
[219] | 52 | {
|
---|
| 53 | cpu0 = cpuSplit = clock();
|
---|
| 54 | elapse0 = elapseSplit = time(0);
|
---|
[3274] | 55 | cpuSecT = cpuSecP = 0.;
|
---|
| 56 | elapSecT = elapSecP = 0;
|
---|
[219] | 57 | }
|
---|
| 58 |
|
---|
[3274] | 59 | //! The destructor call Split() if the object has been created with prtflag=true.
|
---|
| 60 | Timer::~Timer()
|
---|
| 61 | {
|
---|
| 62 | if (defprtflg) Split();
|
---|
| 63 | }
|
---|
[219] | 64 |
|
---|
| 65 |
|
---|
[3274] | 66 | /*! This method updates the CPU and elapsed time.
|
---|
| 67 | If the prfg==true or the constructor prfg==true, it also displays (prints)
|
---|
| 68 | the partial CPU and elapsed time
|
---|
[895] | 69 | An optional message can be passed to be used instead of the
|
---|
| 70 | timer name
|
---|
| 71 | */
|
---|
[3274] | 72 | void Timer::Split(const char* comm, bool prfg)
|
---|
[219] | 73 | {
|
---|
| 74 | time_t elapse = time(0);
|
---|
| 75 | clock_t cpu = clock();
|
---|
| 76 |
|
---|
[3274] | 77 | cpuSecT = ((float)cpu - (float)cpu0) / (float)(CLOCKS_PER_SEC);
|
---|
| 78 | cpuSecP = ((float)cpu - (float)cpuSplit) / (float)(CLOCKS_PER_SEC);
|
---|
[219] | 79 |
|
---|
[3274] | 80 | elapSecP = elapse - elapseSplit;
|
---|
| 81 | elapSecT = elapse - elapse0;
|
---|
[219] | 82 |
|
---|
[3274] | 83 |
|
---|
| 84 | // cumlcpu += (double)cpuSecP;
|
---|
| 85 | // cumulelapse += (int_8)etm;
|
---|
| 86 |
|
---|
| 87 | elapseSplit = elapse;
|
---|
| 88 | cpuSplit = cpu;
|
---|
| 89 |
|
---|
| 90 | if ( !defprtflg && !prfg ) return;
|
---|
| 91 |
|
---|
[895] | 92 | cout << "***Timing " << (comm ? comm : timerName.c_str()) << endl;
|
---|
[219] | 93 |
|
---|
| 94 | // Pour des formats comme ca, la syntaxe printf est plus agreable.
|
---|
| 95 | // Pour ne pas melanger stdio/iostream (pb de desynchronisation sur
|
---|
| 96 | // autres C++ que GNU), on fait un cout << chaine.
|
---|
| 97 |
|
---|
[3274] | 98 | int etm = elapSecP;
|
---|
| 99 | int etmt = elapSecT;
|
---|
[219] | 100 | char out[200];
|
---|
| 101 | sprintf(out,"CPU Time: Total= %g (Partial= %g) Sec.",
|
---|
| 102 | cpuSecT, cpuSecP);
|
---|
| 103 | cout << out << endl;
|
---|
| 104 |
|
---|
| 105 | sprintf(out,"Elapsed Time: Total= %02d:%02d:%02d (Partial=%02d:%02d:%02d)",
|
---|
| 106 | etmt/3600, (etmt%3600)/60, etmt%60,
|
---|
| 107 | etm/3600, (etm%3600)/60, etm%60);
|
---|
| 108 |
|
---|
| 109 | cout << out << endl;
|
---|
| 110 | }
|
---|
| 111 |
|
---|