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>
55 ClassImp(AliCodeTimer)
56 ClassImp(AliCodeTimer::AliPair)
59 AliCodeTimer* AliCodeTimer::fgInstance(0x0);
61 //_____________________________________________________________________________
63 AliCodeTimer::AliPair::Print(Option_t* opt) const
65 // Print timer information
66 cout << opt << Form("%s R:%.4fs C:%.4fs (%d slices)",
67 Name().Data(),Timer()->RealTime(),
68 Timer()->CpuTime(),Timer()->Counter()-1) << endl;
72 //_____________________________________________________________________________
73 AliCodeTimer::AliCodeTimer() : TObject(), fTimers(new TMap)
76 fTimers->SetOwner(kTRUE);
79 //_____________________________________________________________________________
80 AliCodeTimer::~AliCodeTimer()
87 //_____________________________________________________________________________
89 AliCodeTimer::Instance()
91 // single instance of this class
92 if (!fgInstance) fgInstance = new AliCodeTimer;
96 //_____________________________________________________________________________
97 void AliCodeTimer::Continue(const char* classname, const char* methodname,
100 /// Resume a previously stop timer
101 TStopwatch* t = Stopwatch(classname,methodname,message);
108 AliError(Form("No timer for %s/%s/%s",classname,methodname,message));
112 //_____________________________________________________________________________
113 Double_t AliCodeTimer::CpuTime(const char* classname,
114 const char* methodname,
115 const char* message) const
117 /// Return cpu time for a given timer
118 TStopwatch* t = Stopwatch(classname,methodname,message);
129 //_____________________________________________________________________________
131 AliCodeTimer::MethodMap(const char* classname) const
133 /// Return the map for a given "classname"
134 return static_cast<TMap*>(fTimers->GetValue(classname));
137 //_____________________________________________________________________________
139 AliCodeTimer::MessageArray(const char* classname, const char* methodname) const
141 /// Return the array for a given AliPair (classname,methodname)
142 TMap* m = MethodMap(classname);
145 return static_cast<TObjArray*>(m->GetValue(methodname));
150 //_____________________________________________________________________________
151 void AliCodeTimer::PrintMethod(const char* classname, const char* methodname) const
153 /// Print all the timers for a given method
154 TObjArray* messages = MessageArray(classname,methodname);
157 cout << " " << methodname << " ";
159 if ( messages->GetLast() == 0 )
161 AliPair* p = static_cast<AliPair*>(messages->First());
168 TIter next(messages);
171 while ( ( p = static_cast<AliPair*>(next()) ) )
178 //_____________________________________________________________________________
179 void AliCodeTimer::PrintClass(const char* classname) const
181 /// Print all the timers for a given class
182 TMap* methods = MethodMap(classname);
184 TObjString* methodname;
185 TObjArray methodNameArray;
187 while ( ( methodname = static_cast<TObjString*>(next()) ) )
189 methodNameArray.Add(methodname);
192 cout << classname << endl;
194 methodNameArray.Sort();
196 TIter mnext(&methodNameArray);
198 while ( ( methodname = static_cast<TObjString*>(mnext()) ) )
200 PrintMethod(classname,methodname->String().Data());
204 //_____________________________________________________________________________
205 void AliCodeTimer::Print(Option_t* /*opt*/) const
207 /// Print all the timers we hold
209 TObjString* classname;
210 TObjArray classNameArray;
212 while ( ( classname = static_cast<TObjString*>(next()) ) )
214 classNameArray.Add(classname);
217 classNameArray.Sort();
219 TIter cnext(&classNameArray);
220 while ( ( classname = static_cast<TObjString*>(cnext()) ) )
222 PrintClass(classname->String().Data());
226 //_____________________________________________________________________________
228 AliCodeTimer::RealTime(const char* classname, const char* methodname,
229 const char* message) const
231 /// Return real time of a given time
232 TStopwatch* t = Stopwatch(classname,methodname,message);
235 return t->RealTime();
243 //_____________________________________________________________________________
245 AliCodeTimer::Reset()
249 TObjString* classname;
251 while ( ( classname = static_cast<TObjString*>(next()) ) )
253 TMap* m = static_cast<TMap*>(fTimers->GetValue(classname->String().Data()));
257 fTimers->DeleteAll();
260 //_____________________________________________________________________________
262 AliCodeTimer::Start(const char* classname, const char* methodname,
265 /// Start a given time
266 TStopwatch* t = Stopwatch(classname,methodname,message);
269 TMap* m = MethodMap(classname);
274 fTimers->Add(new TObjString(classname),m);
276 TObjArray* messages = MessageArray(classname,methodname);
279 messages = new TObjArray;
280 messages->SetOwner(kTRUE);
281 m->Add(new TObjString(methodname),messages);
286 messages->Add(new AliPair(new TObjString(message),t));
291 //_____________________________________________________________________________
293 AliCodeTimer::Stop(const char* classname, const char* methodname,
296 /// Stop a given timer
297 TStopwatch* t = Stopwatch(classname,methodname,message);
300 AliError(Form("No timer for %s/%s/%s",classname,methodname,message));
308 //_____________________________________________________________________________
310 AliCodeTimer::Stopwatch(const char* classname, const char* methodname,
311 const char* message) const
313 /// Return the internal TStopwatch for a given timer
314 TObjArray* a = MessageArray(classname,methodname);
321 while ( ( p = static_cast<AliPair*>(next()) ) )
323 TString s = p->Name();
324 if ( s == TString(message) )
332 return static_cast<TStopwatch*>(a->First());