1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
7 * Permission to use, copy, modify and distribute this software and its *
8 * documentation strictly for non-commercial purposes is hereby granted *
9 * without fee, provided that the above copyright notice appears in all *
10 * copies and that both the copyright notice and this permission notice *
11 * appear in the supporting documentation. The authors make no claims *
12 * about the suitability of this software for any purpose. It is *
13 * provided "as is" without express or implied warranty. *
14 **************************************************************************/
17 //_________________________________________________________________________
18 // Class to get organized with the way we're timing our methods...
20 // Typical usage is based on macros (like for AliLog related ones AliDebug...)
22 // The idea is to instrument the code with a few macro calls, and then,
23 // at the end of the execution, get a printout of *all* the timers, by using
24 // AliCodeTimer::Instance()->Print()
25 // instead of getting scattered outputs all over the place.
27 // To time a given method, use :
29 // void ClassA::MethodA(....)
31 // AliCodeTimerAuto("")
34 // To get several timers within a same method, use :
36 // void ClassA::MethodB(...)
38 // AliCodeTimerStart("doing something")
40 // AliCodeTimerStop("doing something")
42 // AliCodeTimerStart("doing something else")
44 // AliCodeTimerStop("doing something else")
47 #include "AliCodeTimer.h"
50 #include <TObjString.h>
51 #include <TStopwatch.h>
52 #include <Riostream.h>
57 ClassImp(AliCodeTimer)
58 ClassImp(AliCodeTimer::AliPair)
61 AliCodeTimer* AliCodeTimer::fgInstance(0x0);
63 //_____________________________________________________________________________
65 AliCodeTimer::AliPair::Print(Option_t* opt) const
67 // Print timer information
68 cout << opt << Form("%s R:%.4fs C:%.4fs (%d slices)",
69 Name().Data(),Timer()->RealTime(),
70 Timer()->CpuTime(),Timer()->Counter()-1) << endl;
74 //_____________________________________________________________________________
75 AliCodeTimer::AliCodeTimer() : TObject(), fTimers(new TMap)
78 fTimers->SetOwner(kTRUE);
81 //_____________________________________________________________________________
82 AliCodeTimer::~AliCodeTimer()
89 //_____________________________________________________________________________
91 AliCodeTimer::Instance()
93 // single instance of this class
94 if (!fgInstance) fgInstance = new AliCodeTimer;
98 //_____________________________________________________________________________
99 void AliCodeTimer::Continue(const char* classname, const char* methodname,
102 /// Resume a previously stop timer
103 TStopwatch* t = Stopwatch(classname,methodname,message);
110 AliError(Form("No timer for %s/%s/%s",classname,methodname,message));
114 //_____________________________________________________________________________
115 Double_t AliCodeTimer::CpuTime(const char* classname,
116 const char* methodname,
117 const char* message) const
119 /// Return cpu time for a given timer
120 TStopwatch* t = Stopwatch(classname,methodname,message);
131 //_____________________________________________________________________________
133 AliCodeTimer::MethodMap(const char* classname) const
135 /// Return the map for a given "classname"
136 return static_cast<TMap*>(fTimers->GetValue(classname));
139 //_____________________________________________________________________________
141 AliCodeTimer::MessageArray(const char* classname, const char* methodname) const
143 /// Return the array for a given AliPair (classname,methodname)
144 TMap* m = MethodMap(classname);
147 return static_cast<TObjArray*>(m->GetValue(methodname));
152 //_____________________________________________________________________________
153 void AliCodeTimer::PrintMethod(const char* classname, const char* methodname) const
155 /// Print all the timers for a given method
156 TObjArray* messages = MessageArray(classname,methodname);
159 cout << " " << methodname << " ";
161 if ( messages->GetLast() == 0 )
163 AliPair* p = static_cast<AliPair*>(messages->First());
170 TIter next(messages);
173 while ( ( p = static_cast<AliPair*>(next()) ) )
180 //_____________________________________________________________________________
181 void AliCodeTimer::PrintClass(const char* classname) const
183 /// Print all the timers for a given class
184 TMap* methods = MethodMap(classname);
186 TObjString* methodname;
187 TObjArray methodNameArray;
189 while ( ( methodname = static_cast<TObjString*>(next()) ) )
191 methodNameArray.Add(methodname);
194 cout << classname << endl;
196 methodNameArray.Sort();
198 TIter mnext(&methodNameArray);
200 while ( ( methodname = static_cast<TObjString*>(mnext()) ) )
202 PrintMethod(classname,methodname->String().Data());
206 //_____________________________________________________________________________
207 void AliCodeTimer::Print(Option_t* /*opt*/) const
209 /// Print all the timers we hold
211 TObjString* classname;
212 TObjArray classNameArray;
214 while ( ( classname = static_cast<TObjString*>(next()) ) )
216 classNameArray.Add(classname);
219 classNameArray.Sort();
221 TIter cnext(&classNameArray);
222 while ( ( classname = static_cast<TObjString*>(cnext()) ) )
224 PrintClass(classname->String().Data());
228 //_____________________________________________________________________________
230 AliCodeTimer::RealTime(const char* classname, const char* methodname,
231 const char* message) const
233 /// Return real time of a given time
234 TStopwatch* t = Stopwatch(classname,methodname,message);
237 return t->RealTime();
245 //_____________________________________________________________________________
247 AliCodeTimer::Reset()
251 TObjString* classname;
253 while ( ( classname = static_cast<TObjString*>(next()) ) )
255 TMap* m = static_cast<TMap*>(fTimers->GetValue(classname->String().Data()));
259 fTimers->DeleteAll();
262 //_____________________________________________________________________________
264 AliCodeTimer::Start(const char* classname, const char* methodname,
267 /// Start a given time
268 TStopwatch* t = Stopwatch(classname,methodname,message);
271 TMap* m = MethodMap(classname);
276 fTimers->Add(new TObjString(classname),m);
278 TObjArray* messages = MessageArray(classname,methodname);
281 messages = new TObjArray;
282 messages->SetOwner(kTRUE);
283 m->Add(new TObjString(methodname),messages);
288 messages->Add(new AliPair(new TObjString(message),t));
293 //_____________________________________________________________________________
295 AliCodeTimer::Stop(const char* classname, const char* methodname,
298 /// Stop a given timer
299 TStopwatch* t = Stopwatch(classname,methodname,message);
302 AliError(Form("No timer for %s/%s/%s",classname,methodname,message));
310 //_____________________________________________________________________________
312 AliCodeTimer::Stopwatch(const char* classname, const char* methodname,
313 const char* message) const
315 /// Return the internal TStopwatch for a given timer
316 TObjArray* a = MessageArray(classname,methodname);
323 while ( ( p = static_cast<AliPair*>(next()) ) )
325 TString s = p->Name();
326 if ( s == TString(message) )
334 return static_cast<TStopwatch*>(a->First());