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 **************************************************************************/
19 /// Class to get organized with the way we're timing our methods...
21 /// Typical usage is based on macros (like for AliLog related ones AliDebug...)
23 /// The idea is to instrument the code with a few macro calls, and then,
24 /// at the end of the execution, get a printout of *all* the timers, by using
25 /// AliCodeTimer::Instance()->Print()
26 /// instead of getting scattered outputs all over the place.
28 /// To time a given method, use :
30 /// void ClassA::MethodA(....)
32 /// AliCodeTimerAuto("")
35 /// To get several timers within a same method, use :
37 /// void ClassA::MethodB(...)
39 /// AliCodeTimerStart("doing something")
41 /// AliCodeTimerStop("doing something")
43 /// AliCodeTimerStart("doing something else")
45 /// AliCodeTimerStop("doing something else")
50 #include "AliCodeTimer.h"
53 #include <TObjString.h>
54 #include <TStopwatch.h>
55 #include <Riostream.h>
58 ClassImp(AliCodeTimer)
59 ClassImp(AliCodeTimer::AliPair)
62 AliCodeTimer* AliCodeTimer::fgInstance(0x0);
64 //_____________________________________________________________________________
66 AliCodeTimer::AliPair::Print(Option_t* opt) const
68 // Print timer information
69 cout << opt << Form("%s R:%.4fs C:%.4fs (%d slices)",
70 Name().Data(),Timer()->RealTime(),
71 Timer()->CpuTime(),Timer()->Counter()-1) << endl;
75 //_____________________________________________________________________________
76 AliCodeTimer::AliCodeTimer() : TObject(), fTimers(new TMap)
79 fTimers->SetOwner(kTRUE);
82 //_____________________________________________________________________________
83 AliCodeTimer::~AliCodeTimer()
90 //_____________________________________________________________________________
92 AliCodeTimer::Instance()
94 // single instance of this class
95 if (!fgInstance) fgInstance = new AliCodeTimer;
99 //_____________________________________________________________________________
100 void AliCodeTimer::Continue(const char* classname, const char* methodname,
103 /// Resume a previously stop timer
104 TStopwatch* t = Stopwatch(classname,methodname,message);
111 AliError(Form("No timer for %s/%s/%s",classname,methodname,message));
115 //_____________________________________________________________________________
116 Double_t AliCodeTimer::CpuTime(const char* classname,
117 const char* methodname,
118 const char* message) const
120 /// Return cpu time for a given timer
121 TStopwatch* t = Stopwatch(classname,methodname,message);
132 //_____________________________________________________________________________
134 AliCodeTimer::MethodMap(const char* classname) const
136 /// Return the map for a given "classname"
137 return static_cast<TMap*>(fTimers->GetValue(classname));
140 //_____________________________________________________________________________
142 AliCodeTimer::MessageArray(const char* classname, const char* methodname) const
144 /// Return the array for a given AliPair (classname,methodname)
145 TMap* m = MethodMap(classname);
148 return static_cast<TObjArray*>(m->GetValue(methodname));
153 //_____________________________________________________________________________
154 void AliCodeTimer::PrintMethod(const char* classname, const char* methodname) const
156 /// Print all the timers for a given method
157 TObjArray* messages = MessageArray(classname,methodname);
160 cout << " " << methodname << " ";
162 if ( messages->GetLast() == 0 )
164 AliPair* p = static_cast<AliPair*>(messages->First());
171 TIter next(messages);
174 while ( ( p = static_cast<AliPair*>(next()) ) )
181 //_____________________________________________________________________________
182 void AliCodeTimer::PrintClass(const char* classname) const
184 /// Print all the timers for a given class
185 TMap* methods = MethodMap(classname);
187 TObjString* methodname;
188 TObjArray methodNameArray;
190 while ( ( methodname = static_cast<TObjString*>(next()) ) )
192 methodNameArray.Add(methodname);
195 cout << classname << endl;
197 methodNameArray.Sort();
199 TIter mnext(&methodNameArray);
201 while ( ( methodname = static_cast<TObjString*>(mnext()) ) )
203 PrintMethod(classname,methodname->String().Data());
207 //_____________________________________________________________________________
208 void AliCodeTimer::Print(Option_t* /*opt*/) const
210 /// Print all the timers we hold
212 TObjString* classname;
213 TObjArray classNameArray;
215 while ( ( classname = static_cast<TObjString*>(next()) ) )
217 classNameArray.Add(classname);
220 classNameArray.Sort();
222 TIter cnext(&classNameArray);
223 while ( ( classname = static_cast<TObjString*>(cnext()) ) )
225 PrintClass(classname->String().Data());
229 //_____________________________________________________________________________
231 AliCodeTimer::RealTime(const char* classname, const char* methodname,
232 const char* message) const
234 /// Return real time of a given time
235 TStopwatch* t = Stopwatch(classname,methodname,message);
238 return t->RealTime();
246 //_____________________________________________________________________________
248 AliCodeTimer::Reset()
252 TObjString* classname;
254 while ( ( classname = static_cast<TObjString*>(next()) ) )
256 TMap* m = static_cast<TMap*>(fTimers->GetValue(classname->String().Data()));
260 fTimers->DeleteAll();
263 //_____________________________________________________________________________
265 AliCodeTimer::Start(const char* classname, const char* methodname,
268 /// Start a given time
269 TStopwatch* t = Stopwatch(classname,methodname,message);
272 TMap* m = MethodMap(classname);
277 fTimers->Add(new TObjString(classname),m);
279 TObjArray* messages = MessageArray(classname,methodname);
282 messages = new TObjArray;
283 messages->SetOwner(kTRUE);
284 m->Add(new TObjString(methodname),messages);
289 messages->Add(new AliPair(new TObjString(message),t));
294 //_____________________________________________________________________________
296 AliCodeTimer::Stop(const char* classname, const char* methodname,
299 /// Stop a given timer
300 TStopwatch* t = Stopwatch(classname,methodname,message);
303 AliError(Form("No timer for %s/%s/%s",classname,methodname,message));
311 //_____________________________________________________________________________
313 AliCodeTimer::Stopwatch(const char* classname, const char* methodname,
314 const char* message) const
316 /// Return the internal TStopwatch for a given timer
317 TObjArray* a = MessageArray(classname,methodname);
324 while ( ( p = static_cast<AliPair*>(next()) ) )
326 TString s = p->Name();
327 if ( s == TString(message) )
335 return static_cast<TStopwatch*>(a->First());