[219] | 1 | //
|
---|
[3638] | 2 | // $Id: ctimer.cc,v 1.10 2009-05-26 21:39:54 ansari Exp $
|
---|
[219] | 3 | //
|
---|
| 4 |
|
---|
[3579] | 5 | #include "machdefs.h"
|
---|
[219] | 6 | #include "ctimer.h"
|
---|
| 7 |
|
---|
[3579] | 8 | #include <iostream>
|
---|
| 9 | #include <stdio.h>
|
---|
| 10 | #include <string>
|
---|
| 11 |
|
---|
[219] | 12 | //++
|
---|
| 13 | // Class Timer
|
---|
[895] | 14 | // Lib SysTools
|
---|
[219] | 15 | // include ctimer.h
|
---|
| 16 | //
|
---|
| 17 | // Chronométrage de programmes. Le constructeur mémorise l'heure et
|
---|
| 18 | // le temps CPU, ainsi qu'un message éventuel.
|
---|
| 19 | //
|
---|
| 20 | // Split affiche le temps partiel.
|
---|
| 21 | //
|
---|
| 22 | // Le destructeur affiche le temps total (CPU, et écoulé).
|
---|
| 23 | //
|
---|
| 24 | // Des macros permettent une utilisation simplifiée :
|
---|
| 25 | // * TIMEF crée un objet de type Timer, avec le nom de la function
|
---|
| 26 | // courante comme message. Le temps écoulé sera affiché à la sortie
|
---|
| 27 | // de la function.
|
---|
| 28 | //--
|
---|
| 29 |
|
---|
| 30 |
|
---|
| 31 |
|
---|
[895] | 32 | /*!
|
---|
| 33 | \class SOPHYA::Timer
|
---|
[913] | 34 | \ingroup SysTools
|
---|
[2598] | 35 | \brief Simple chronometer for CPU and elapsed time measurements.
|
---|
[895] | 36 | This class implements a simple chronometer which can be used for
|
---|
| 37 | measuring the CPU and elapsed time in functions. The constructor
|
---|
| 38 | keeps the start time (and CPU time) and an optional message.
|
---|
[3274] | 39 | The \b Split updates partial times and optionaly displays partial and
|
---|
| 40 | total CPU and elapsed time.
|
---|
| 41 | The destructor calls Split() if the object has been created with prfg=true.
|
---|
[895] | 42 | displays the total CPU and elapsed time since timer creation.
|
---|
| 43 | The macro \b TIMEF create a timer object with the function name.
|
---|
[3274] | 44 | The elapsed and CPU time are displayed at the end of the bloc or function.
|
---|
[895] | 45 | */
|
---|
| 46 |
|
---|
[3636] | 47 | namespace SOPHYA {
|
---|
[3638] | 48 | //! Default constructor with optional default print flag
|
---|
| 49 | Timer::Timer(bool prfg)
|
---|
| 50 | : defprtflg(prfg)
|
---|
| 51 | {
|
---|
| 52 | cpu0 = cpuSplit = clock();
|
---|
| 53 | gettimeofday(&elapse0, NULL);
|
---|
| 54 | elapseSplit = elapse0;
|
---|
| 55 | // elapse0 = elapseSplit = time(0);
|
---|
| 56 | cpuSecT = cpuSecP = 0.;
|
---|
| 57 | elapSecT = elapSecP = 0;
|
---|
| 58 | }
|
---|
[3274] | 59 | /*!
|
---|
[3638] | 60 | \brief Constructor with the specification of a name or message
|
---|
| 61 | and optional default print flag.
|
---|
[3274] | 62 | if \b prfg==true , a call to Split() causes the display of partial and
|
---|
| 63 | total CPU and elapsed time.
|
---|
| 64 | */
|
---|
| 65 | Timer::Timer(const char* name, bool prfg)
|
---|
[3638] | 66 | : timerName(((name!=NULL)?name:"")) , defprtflg(prfg)
|
---|
[219] | 67 | {
|
---|
| 68 | cpu0 = cpuSplit = clock();
|
---|
[3579] | 69 | gettimeofday(&elapse0, NULL);
|
---|
| 70 | elapseSplit = elapse0;
|
---|
| 71 | // elapse0 = elapseSplit = time(0);
|
---|
[3274] | 72 | cpuSecT = cpuSecP = 0.;
|
---|
| 73 | elapSecT = elapSecP = 0;
|
---|
[219] | 74 | }
|
---|
| 75 |
|
---|
[3274] | 76 | //! The destructor call Split() if the object has been created with prtflag=true.
|
---|
| 77 | Timer::~Timer()
|
---|
| 78 | {
|
---|
| 79 | if (defprtflg) Split();
|
---|
| 80 | }
|
---|
[219] | 81 |
|
---|
| 82 |
|
---|
[3274] | 83 | /*! This method updates the CPU and elapsed time.
|
---|
| 84 | If the prfg==true or the constructor prfg==true, it also displays (prints)
|
---|
| 85 | the partial CPU and elapsed time
|
---|
[895] | 86 | An optional message can be passed to be used instead of the
|
---|
| 87 | timer name
|
---|
| 88 | */
|
---|
[3636] | 89 | void Timer::SplitQ()
|
---|
[219] | 90 | {
|
---|
[3579] | 91 | struct timeval elapse;
|
---|
| 92 | gettimeofday(&elapse, NULL);
|
---|
| 93 |
|
---|
[219] | 94 | clock_t cpu = clock();
|
---|
| 95 |
|
---|
[3579] | 96 | uint_8 deltacpu;
|
---|
| 97 | if (cpu < cpuSplit) {
|
---|
| 98 | clock_t cpumax = 0; cpumax = ~cpumax;
|
---|
| 99 | deltacpu = (uint_8)(cpumax-cpuSplit)+cpu;
|
---|
| 100 | }
|
---|
| 101 | else deltacpu = cpu - cpuSplit;
|
---|
[219] | 102 |
|
---|
[3579] | 103 | cpuSecT += ((double)deltacpu) / (double)(CLOCKS_PER_SEC);
|
---|
| 104 | cpuSecP = ((double)deltacpu) / (double)(CLOCKS_PER_SEC);
|
---|
| 105 |
|
---|
| 106 | elapSecP = (elapse.tv_sec - elapseSplit.tv_sec)*1000
|
---|
| 107 | + (elapse.tv_usec - elapseSplit.tv_usec)/1000;
|
---|
| 108 | elapSecT = (elapse.tv_sec - elapse0.tv_sec)*1000
|
---|
| 109 | + (elapse.tv_usec - elapse0.tv_usec)/1000;
|
---|
[219] | 110 |
|
---|
[3274] | 111 |
|
---|
| 112 | // cumlcpu += (double)cpuSecP;
|
---|
| 113 | // cumulelapse += (int_8)etm;
|
---|
| 114 |
|
---|
| 115 | elapseSplit = elapse;
|
---|
| 116 | cpuSplit = cpu;
|
---|
[3636] | 117 | }
|
---|
[3274] | 118 |
|
---|
[3636] | 119 | void Timer::Split(const char* comm, bool prfg)
|
---|
| 120 | {
|
---|
| 121 | SplitQ();
|
---|
[3274] | 122 | if ( !defprtflg && !prfg ) return;
|
---|
[895] | 123 | cout << "***Timing " << (comm ? comm : timerName.c_str()) << endl;
|
---|
[3636] | 124 | Print(cout);
|
---|
| 125 | }
|
---|
[219] | 126 |
|
---|
| 127 | // Pour des formats comme ca, la syntaxe printf est plus agreable.
|
---|
| 128 | // Pour ne pas melanger stdio/iostream (pb de desynchronisation sur
|
---|
| 129 | // autres C++ que GNU), on fait un cout << chaine.
|
---|
| 130 |
|
---|
[3636] | 131 |
|
---|
| 132 | ostream& Timer::Print(ostream& os) const
|
---|
| 133 | {
|
---|
| 134 |
|
---|
[3579] | 135 | int_4 etm = elapSecP/1000;
|
---|
| 136 | int_4 etmt = elapSecT/1000;
|
---|
| 137 |
|
---|
| 138 | char out[200],outp[64];
|
---|
| 139 | sprintf(out,"CPU Time: Total= %lg (Partial= %lg) s",
|
---|
[219] | 140 | cpuSecT, cpuSecP);
|
---|
[3636] | 141 | os << out << endl;
|
---|
[219] | 142 |
|
---|
[3579] | 143 | if (etmt<60) {
|
---|
| 144 | int_4 etmtms = elapSecT%1000;
|
---|
[3636] | 145 | sprintf(out,"%02d.%03d s", etmt, etmtms);
|
---|
[3579] | 146 | }
|
---|
| 147 | else
|
---|
| 148 | sprintf(out,"%02d:%02d:%02d", etmt/3600, (etmt%3600)/60, etmt%60);
|
---|
[219] | 149 |
|
---|
[3579] | 150 | if (etm<60) {
|
---|
| 151 | int_4 etmms = elapSecT%1000;
|
---|
[3636] | 152 | sprintf(outp,"%02d.%03d s", etm, etmms);
|
---|
[3579] | 153 | }
|
---|
| 154 | else
|
---|
| 155 | sprintf(outp,"%02d:%02d:%02d", etm/3600, (etm%3600)/60, etm%60);
|
---|
| 156 |
|
---|
[3636] | 157 | os << "Elapsed Time: Total=" << out << " (Partial= " << outp << ")" << endl;
|
---|
[219] | 158 | }
|
---|
| 159 |
|
---|
[3636] | 160 | } // FIN namespace SOPHYA
|
---|