source: Sophya/trunk/SophyaLib/SysTools/ctimer.h@ 4007

Last change on this file since 4007 was 3638, checked in by ansari, 16 years ago

Correction bug plantage Timer(NULL) - Reza 26/05/2009

File size: 3.0 KB
RevLine 
[219]1// This may look like C code, but it is really -*- C++ -*-
2//
[3638]3// $Id: ctimer.h,v 1.8 2009-05-26 21:39:55 ansari Exp $
[219]4//
5
6
7#ifndef CTIMER_SEEN
8#define CTIMER_SEEN
9
[895]10#include "machdefs.h"
[219]11#include <sys/types.h>
12#include <time.h>
[3579]13#include <sys/time.h>
[895]14#include <string>
[3636]15#include <iostream>
[219]16
[3579]17using namespace std;
18
[219]19// <summary> Permet de chronometrer des fonctions. </summary>
[3274]20// Class Timer qui memorise l'heure de sa creation, et le temps CPU.
[219]21// A la fin du bloc ou de la procedure, l'objet est detruit,
22// et son destructeur affiche le temps ecoule.
23
[895]24namespace SOPHYA {
25
26//! Simple chronometer class
[241]27class Timer {
[219]28public:
[3638]29 Timer(bool prfg=true);
[219]30 // L'objet memorise le temps CPU et l'heure, et le nom donne
[3638]31 Timer(const char* name, bool prfg=true);
[219]32
33 // Le destructeur appelle split sans parametre.
34 virtual ~Timer();
35
36 // Affiche le temps ecoule total/partiel, avec le nom eventuel.
37 // Si pas de parametre affiche le nom donne a la creation.
[3274]38 void Split(const char* comm=0, bool prfg=false);
[219]39
[3636]40 //! store partial CPU and elapsed time (no print)
41 void SplitQ();
42
[219]43 // Sert a eviter que GNU ne pretende qu'on utilise pas l'objet...
[895]44 /*! To avoid not used object compiler warnings */
[219]45 void Nop() {}
46
[3274]47 /*! \brief Return the total elapsed time (number of seconds),
48 between the object creation and the last call to Split().
49 */
[3579]50 inline int_8 TotalElapsedTime() { return elapSecT/1000 ; }
[3274]51 /*! \brief Return the partial elapsed time (number of seconds).
52 between the last two calls to Split().
53 */
[3579]54 inline int_8 PartialElapsedTime() { return elapSecP/1000 ; }
55
56 /*! \brief Return the total elapsed time (in milli-seconds),
57 between the object creation and the last call to Split().
58 */
59 inline int_8 TotalElapsedTimems() { return elapSecT ; }
60 /*! \brief Return the partial elapsed time (in milli-seconds).
61 between the last two calls to Split().
62 */
63 inline int_8 PartialElapsedTimems() { return elapSecP ; }
[3274]64
65 /*! \brief Return the total CPU time in seconds,
66 between the object creation and the last call to Split().
67 */
[3579]68 inline double TotalCPUTime() { return cpuSecT; }
[3274]69 /*! \brief Return the partial CPU time in seconds,
70 between the last two calls to Split().
71 */
[3579]72 inline double PartialCPUTime() { return cpuSecP; }
[3274]73
[3636]74//! Print the CPU and elapsed time on os (partial time corresponding to the last call to Split())
75 ostream& Print(ostream& os) const ;
76//! Print the CPU and elapsed time on cout
77 inline ostream& Print() const { return Print(cout); }
78
[219]79private:
80 clock_t cpu0, cpuSplit;
[3579]81 struct timeval elapse0, elapseSplit;
82 double cpuSecT, cpuSecP; // Total and partial CPU time
83 int_8 elapSecT, elapSecP; // Total and partial elapsed time in ms
[895]84 string timerName;
[3274]85 bool defprtflg;
[219]86};
87
[3636]88/*! operator << overloading - Calls SplitQ() and Print() */
89inline ostream& operator << (ostream& s, Timer& tm)
90 { tm.SplitQ(); tm.Print(s); return(s); }
91
[895]92} // namespace SOPHYA
93
[3274]94#define TIMEF Timer timer(__PRETTY_FUNCTION__); timer.Nop();
[219]95
96#endif // CTIMER_SEEN
Note: See TracBrowser for help on using the repository browser.