AliRoot Core  3dc7879 (3dc7879)
AliCodeTimer.h
Go to the documentation of this file.
1 #ifndef ALICODETIMER_H
2 #define ALICODETIMER_H
3 
4 /* Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
5 * See cxx source for full Copyright notice */
6 
7 // $Id$
8 
12 // Author Laurent Aphecetche
13 
14 #ifndef ROOT_TString
15 # include "TString.h"
16 #endif
17 #ifndef ROOT_TObjString
18 # include "TObjString.h"
19 #endif
20 #ifndef ALILOG_H
21 # include "AliLog.h"
22 #endif
23 
24 class TStopwatch;
25 class TMap;
26 
27 class AliCodeTimer : public TObject
28 {
29 public:
30 
31  AliCodeTimer();
32  virtual ~AliCodeTimer();
33 
35  static AliCodeTimer* Instance();
36 
38  void Continue(const char* classname, const char* methodname, const char* message="");
39 
41  Double_t CpuTime(const char* classname, const char* methodname, const char* message="") const;
42 
44  void Print(Option_t* opt="") const;
45 
47  Double_t RealTime(const char* classname, const char* methodname, const char* message="") const;
48 
50  void Reset();
51 
53  void Start(const char* classname, const char* methodname, const char* message="");
54 
56  void Stop(const char* classname, const char* methodname, const char* message="");
57 
58 public:
59 
60  class AliPair : public TObject
61  {
62  public:
63  AliPair() : TObject(),fName(0), fTimer(0) {}
64  // ctor
65  AliPair(TObjString* name, TStopwatch* timer) : TObject(), fName(name), fTimer(timer) {}
66  virtual ~AliPair() { delete fName; }
67 
69  TString Name() const { return fName->String(); }
71  TStopwatch* Timer() const { return fTimer; }
72 
74  virtual Bool_t IsSortable() const { return kTRUE; }
76  virtual Int_t Compare(const TObject* object) const
77  { return fName->Compare(((const AliPair*)(object))->fName); }
78 
79  virtual void Print(Option_t* opt="") const;
80 
81 private:
82  AliPair(const AliPair&);
83  AliPair& operator=(const AliPair&);
84 
85  TObjString* fName; // name of the timer
86  TStopwatch* fTimer; // actual timer
87 
88  ClassDef(AliPair,1) // internal class to hold (string,TStopwatch*) AliPair
89  };
90 
91  class AliAutoPtr
92  {
93  public:
94 
96  AliAutoPtr(const char* classname, const char* methodname, const char* message="")
97  : fA(classname), fB(methodname), fC(message)
98  { AliCodeTimer::Instance()->Start(classname,methodname,message); }
99 
101  ~AliAutoPtr() { AliCodeTimer::Instance()->Stop(fA.Data(),fB.Data(),fC.Data()); }
102 
103  private:
104  TString fA; // first id
105  TString fB; // second id
106  TString fC; // third id
107  };
108 
109 private:
110 
111  TMap* MethodMap(const char* classname) const;
112  TObjArray* MessageArray(const char* classname, const char* methodname) const;
113  TStopwatch* Stopwatch(const char* classname, const char* methodname, const char* message="") const;
114  void PrintClass(const char* classname) const;
115  void PrintMethod(const char* classname, const char* methodname) const;
116 
117 private:
118 
119  AliCodeTimer(const AliCodeTimer& rhs);
120  AliCodeTimer& operator=(const AliCodeTimer& rhs);
121 
122  static AliCodeTimer* fgInstance; //< unique instance
123 
124  TMap* fTimers; //< internal timers
125 
126  ClassDef(AliCodeTimer,1) // A timer holder
127 };
128 
129 #ifndef LOG_NO_DEBUG
130 
131 #define AliCodeTimerStartClass(message) AliCodeTimer::Instance()->Start(Class()->GetName(),FUNCTIONNAME(),message);
132 #define AliCodeTimerStopClass(message) AliCodeTimer::Instance()->Stop(Class()->GetName(),FUNCTIONNAME(),message);
133 #define AliCodeTimerAutoClass(message,counter) AliCodeTimer::AliAutoPtr aliCodeTimerAliAutoPtrVariable##counter(Class()->GetName(),FUNCTIONNAME(),message);
134 
135 #define AliCodeTimerStart(message) AliCodeTimer::Instance()->Start(ClassName(),FUNCTIONNAME(),message);
136 #define AliCodeTimerStop(message) AliCodeTimer::Instance()->Stop(ClassName(),FUNCTIONNAME(),message);
137 #define AliCodeTimerAuto(message,counter) AliCodeTimer::AliAutoPtr aliCodeTimerAliAutoPtrVariable##counter(ClassName(),FUNCTIONNAME(),message);
138 
139 #define AliCodeTimerStartGeneral(message) AliCodeTimer::Instance()->Start("General",FUNCTIONNAME(),message);
140 #define AliCodeTimerStopGeneral(message) AliCodeTimer::Instance()->Stop("General",FUNCTIONNAME(),message);
141 #define AliCodeTimerAutoGeneral(message,counter) AliCodeTimer::AliAutoPtr aliCodeTimerAliAutoPtrVariable##counter("General",FUNCTIONNAME(),message);
142 
143 #else
144 
145 #define AliCodeTimerStartClass(message)
146 #define AliCodeTimerStopClass(message)
147 #define AliCodeTimerAutoClass(message,counter)
148 
149 #define AliCodeTimerStart(message)
150 #define AliCodeTimerStop(message)
151 #define AliCodeTimerAuto(message,counter)
152 
153 #define AliCodeTimerStartGeneral(message)
154 #define AliCodeTimerStopGeneral(message)
155 #define AliCodeTimerAutoGeneral(message,counter)
156 
157 #endif
158 
159 #endif
void PrintClass(const char *classname) const
AliPair(TObjString *name, TStopwatch *timer)
Definition: AliCodeTimer.h:65
TObjString * fName
Definition: AliCodeTimer.h:85
void Print(Option_t *opt="") const
Print the list of timers we manage.
TMap * MethodMap(const char *classname) const
TStopwatch * Timer() const
get timer
Definition: AliCodeTimer.h:71
#define TObjArray
TStopwatch timer
Definition: kNNSpeed.C:15
TStopwatch * fTimer
Definition: AliCodeTimer.h:86
TString Name() const
get name
Definition: AliCodeTimer.h:69
virtual Bool_t IsSortable() const
we are sortable (by name)
Definition: AliCodeTimer.h:74
virtual void Print(Option_t *opt="") const
Double_t CpuTime(const char *classname, const char *methodname, const char *message="") const
Return the cpu time spent in timer(classname,methodname,message)
void Continue(const char *classname, const char *methodname, const char *message="")
Continue timer(classname,methodname,message)
virtual Int_t Compare(const TObject *object) const
compare the names
Definition: AliCodeTimer.h:76
TObjArray * MessageArray(const char *classname, const char *methodname) const
TStopwatch * Stopwatch(const char *classname, const char *methodname, const char *message="") const
void PrintMethod(const char *classname, const char *methodname) const
static AliCodeTimer * fgInstance
Definition: AliCodeTimer.h:122
void Stop(const char *classname, const char *methodname, const char *message="")
Stop timer(classname,methodname,message)
static AliCodeTimer * Instance()
Unique instance of this class, which is a singleton.
AliAutoPtr(const char *classname, const char *methodname, const char *message="")
ctor
Definition: AliCodeTimer.h:96
void Reset()
Reset all our timers.
Double_t RealTime(const char *classname, const char *methodname, const char *message="") const
Return the real time spent in timer(classname,methodname,message)
TMap * fTimers
Definition: AliCodeTimer.h:124
AliPair & operator=(const AliPair &)
void Start(const char *classname, const char *methodname, const char *message="")
Start timer(classname,methodname,message)
virtual ~AliCodeTimer()