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