Changeset 3274 in Sophya


Ignore:
Timestamp:
Jul 6, 2007, 2:54:12 PM (18 years ago)
Author:
ansari
Message:

Ajout methodes d'acces au temps CPU et elapsed ds la classe Timer , Reza 05/07/2007

Location:
trunk/SophyaLib/SysTools
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/SophyaLib/SysTools/ctimer.cc

    r2615 r3274  
    11//
    2 // $Id: ctimer.cc,v 1.6 2004-09-10 09:54:57 cmv Exp $
     2// $Id: ctimer.cc,v 1.7 2007-07-06 12:54:12 ansari Exp $
    33//
    44
     
    2323//      courante comme message. Le temps écoulé sera affiché à la sortie
    2424//      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.
    2725//--
    2826
    2927
    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 //--
    3928
    4029/*!
     
    4534  measuring the CPU and elapsed time in functions. The constructor
    4635  keeps the start time (and CPU time) and an optional message.
    47   The \b Split method displays the partial time, and destructor
     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.
    4839  displays the total CPU and elapsed time since timer creation.
    4940  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)
     41  The elapsed and CPU time are displayed at the end of the bloc or function.
    5242*/
    5343
    54 /*! Constructor with the specification of a optional name or message */
    55 Timer::Timer(const char* name)
    56 : timerName(name)
     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*/
     50Timer::Timer(const char* name, bool prfg)
     51  : timerName(name) , defprtflg(prfg)
    5752{
    5853  cpu0 = cpuSplit = clock();
    5954  elapse0 = elapseSplit = time(0);
     55  cpuSecT = cpuSecP = 0.;
     56  elapSecT = elapSecP = 0;
    6057}
    6158
    62 //++
    63 // Titre        Méthodes
    64 //--
     59//! The destructor call Split() if the object has been created with prtflag=true.
     60Timer::~Timer()
     61{
     62  if (defprtflg) Split();
     63}
    6564
    66 //++
    67 // Timer::Split(const char* comm)
    68 //      Affiche le temps partiel.
    69 //--
    7065
    71 /*! Method which displays the partial CPU and elapsed time
     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
    7269    An optional message can be passed to be used instead of the
    7370    timer name
    7471*/
    75 void Timer::Split(const char* comm)
     72void Timer::Split(const char* comm, bool prfg)
    7673{
    7774  time_t elapse = time(0);
    7875  clock_t cpu   = clock();
    7976
    80   float cpuSecT = ((float)cpu - (float)cpu0) / (float)(CLOCKS_PER_SEC);
    81   float cpuSecP = ((float)cpu - (float)cpuSplit) / (float)(CLOCKS_PER_SEC);
     77  cpuSecT = ((float)cpu - (float)cpu0) / (float)(CLOCKS_PER_SEC);
     78  cpuSecP = ((float)cpu - (float)cpuSplit) / (float)(CLOCKS_PER_SEC);
    8279 
    83   int etm  = elapse - elapseSplit;
    84   int etmt = elapse - elapse0;
     80  elapSecP  = elapse - elapseSplit;
     81  elapSecT = elapse - elapse0;
     82
     83
     84  //  cumlcpu += (double)cpuSecP;
     85  //  cumulelapse += (int_8)etm;
     86
     87  elapseSplit = elapse;
     88  cpuSplit    = cpu;
     89
     90  if ( !defprtflg && !prfg )  return;
    8591
    8692  cout << "***Timing " << (comm ? comm : timerName.c_str()) << endl;
     
    9096// autres C++ que GNU), on fait un cout << chaine.
    9197
     98  int etm  = elapSecP;
     99  int etmt = elapSecT;
    92100  char out[200];
    93101  sprintf(out,"CPU     Time: Total= %g (Partial= %g) Sec.",
     
    100108
    101109  cout << out << endl;
    102 
    103   elapseSplit = elapse;
    104   cpuSplit    = cpu;
    105110}
    106111
    107 Timer::~Timer()
    108 {
    109   Split();
    110 }
  • trunk/SophyaLib/SysTools/ctimer.h

    r2322 r3274  
    11// This may look like C code, but it is really -*- C++ -*-
    22//
    3 // $Id: ctimer.h,v 1.4 2003-02-11 15:31:07 cmv Exp $
     3// $Id: ctimer.h,v 1.5 2007-07-06 12:54:12 ansari Exp $
    44//
    55
     
    1616
    1717// <summary> Permet de chronometrer des fonctions. </summary>
    18 // La macro TIMEF cree un objet de class Timer qui memorise
    19 // l'heure de sa creation, et le temps CPU.
    20 
     18// Class Timer qui memorise l'heure de sa creation, et le temps CPU.
    2119// A la fin du bloc ou de la procedure, l'objet est detruit,
    2220// et son destructeur affiche le temps ecoule.
    2321
    24 // La macro SPLITTIME lui permet d'afficher des temps partiels.
    2522namespace SOPHYA {
    2623
     
    2926public:
    3027  // L'objet memorise le temps CPU et l'heure, et le nom donne
    31   Timer(const char* name=0);
     28  Timer(const char* name=0, bool prfg=true);
    3229
    3330  // Le destructeur appelle split sans parametre.
     
    3633  // Affiche le temps ecoule total/partiel, avec le nom eventuel.
    3734  // Si pas de parametre affiche le nom donne a la creation.
    38   void Split(const char* comm=0);
     35  void Split(const char* comm=0, bool prfg=false);
    3936
    4037  // Sert a eviter que GNU ne pretende qu'on utilise pas l'objet...
     
    4239  void Nop() {}
    4340
     41  /*! \brief Return the total elapsed time (number of seconds),
     42    between the object creation and the last call to Split().
     43  */
     44  inline int_8 TotalElapsedTime() { return elapSecT ; }
     45  /*! \brief Return the partial elapsed time (number of seconds).
     46      between the last two calls to Split().
     47  */
     48  inline int_8 PartialElapsedTime() { return elapSecP ; }
     49
     50  /*! \brief Return the total CPU time in seconds,
     51    between the object creation and the last call to Split().
     52  */
     53  inline float TotalCPUTime() { return  cpuSecT; }
     54  /*! \brief Return the partial CPU time in seconds,
     55      between the last two calls to Split().
     56  */
     57  inline float PartialCPUTime() { return  cpuSecP; }
     58
    4459private:
    4560  clock_t cpu0, cpuSplit;
    4661  time_t  elapse0, elapseSplit;
     62  float cpuSecT, cpuSecP;     // Total and partial CPU time
     63  int_8 elapSecT, elapSecP;    // Total and partial elapsed time
    4764  string timerName;
     65  bool defprtflg;
    4866};
    4967
    5068} // namespace SOPHYA
    5169
    52 #define TIMEN(x)  Timer timer(x); timer.Nop();
    53 #define TIMEF     Timer timer(__PRETTY_FUNCTION__); timer.Nop();
    54 #define SPLITTIME timer.Split();
     70#define TIMEF     Timer timer(__PRETTY_FUNCTION__); timer.Nop();
    5571
    5672#endif // CTIMER_SEEN
Note: See TracChangeset for help on using the changeset viewer.