Panda3D
profileTimer.h
1 // Filename: profileTimer.h
2 // Created by: skyler
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 #ifndef PROFILETIMER_H //[
15 #define PROFILETIMER_H
16 
17 #include "pandabase.h"
18 #include "trueClock.h"
19 
20 /*
21  ProfileTimer
22 
23  HowTo:
24  Create a ProfileTimer and hold onto it.
25  Call init() whenever you like (the timer doesn't
26  start yet).
27  Call on() to start the timer.
28  While the timer is on, call mark() at each point of interest,
29  in the code you are timing.
30  You can turn the timer off() and on() to skip things you
31  don't want to time.
32  When your timing is finished, call printTo() to see the
33  results (e.g. myTimer.printTo(cerr)).
34 
35  Notes:
36  You should be able to time things down to the millisecond
37  well enough, but if you call on() and off() within micro-
38  seconds of each other, I don't think you'll get very good
39  results.
40 */
41 class EXPCL_PANDAEXPRESS ProfileTimer {
42  enum { MaxEntriesDefault=4096 };
43 PUBLISHED:
44  ProfileTimer(const char* name=0, int maxEntries=MaxEntriesDefault);
45  ProfileTimer(const ProfileTimer& other);
46  ~ProfileTimer();
47 
48  void init(const char* name, int maxEntries=MaxEntriesDefault);
49 
50  void on();
51  void mark(const char* tag);
52  void off();
53  void off(const char* tag);
54 
55  // Don't call any of the following during timing:
56  // (Because they are slow, not because anything will break).
57  double getTotalTime() const;
58  static void consolidateAllTo(ostream &out=cout);
59  void consolidateTo(ostream &out=cout) const;
60  static void printAllTo(ostream &out=cout);
61  void printTo(ostream &out=cout) const;
62 
63 public:
64  /*
65  e.g.
66  void Foo() {
67  ProfileTimer::AutoTimer(myProfiler, "Foo()");
68  ...
69  }
70  */
71  class EXPCL_PANDAEXPRESS AutoTimer {
72  public:
73  AutoTimer(ProfileTimer& profile, const char* tag);
74  ~AutoTimer();
75 
76  protected:
77  ProfileTimer& _profile;
78  const char* _tag;
79  };
80 
81 protected:
82  static ProfileTimer* _head;
83  ProfileTimer* _next;
84  class TimerEntry {
85  public:
86  const char* _tag; // not owned by this.
87  double _time;
88  };
89  double _on;
90  double _elapsedTime;
91  const char* _name; // not owned by this.
92  int _maxEntries;
93  int _entryCount;
94  TimerEntry* _entries;
95  int _autoTimerCount; // see class AutoTimer
96 
97  double getTime();
98 
99  friend class ProfileTimer::AutoTimer;
100 };
101 
102 #include "profileTimer.I"
103 
104 #endif //]