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

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

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

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