Panda3D
 All Classes Functions Variables Enumerations
profileTimer.h
00001 // Filename: profileTimer.h
00002 // Created by: skyler 
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) Carnegie Mellon University.  All rights reserved.
00008 //
00009 // All use of this software is subject to the terms of the revised BSD
00010 // license.  You should have received a copy of this license along
00011 // with this source code in a file named "LICENSE."
00012 //
00013 ////////////////////////////////////////////////////////////////////
00014 #ifndef PROFILETIMER_H //[
00015 #define PROFILETIMER_H
00016 
00017 #include "pandabase.h"
00018 #include "trueClock.h"
00019 
00020 /*
00021     ProfileTimer
00022 
00023     HowTo:
00024       Create a ProfileTimer and hold onto it.
00025       Call init() whenever you like (the timer doesn't
00026         start yet).
00027       Call on() to start the timer.
00028       While the timer is on, call mark() at each point of interest,
00029         in the code you are timing.
00030       You can turn the timer off() and on() to skip things you
00031         don't want to time.
00032       When your timing is finished, call printTo() to see the
00033         results (e.g. myTimer.printTo(cerr)).
00034 
00035     Notes:
00036       You should be able to time things down to the millisecond
00037       well enough, but if you call on() and off() within micro-
00038       seconds of each other, I don't think you'll get very good
00039       results.
00040 */
00041 class EXPCL_PANDAEXPRESS ProfileTimer {
00042   enum { MaxEntriesDefault=4096 };
00043 PUBLISHED:
00044   ProfileTimer(const char* name=0, int maxEntries=MaxEntriesDefault);
00045   ProfileTimer(const ProfileTimer& other);
00046   ~ProfileTimer();
00047 
00048   void init(const char* name, int maxEntries=MaxEntriesDefault);
00049 
00050   void on();
00051   void mark(const char* tag);
00052   void off();
00053   void off(const char* tag);
00054 
00055   // Don't call any of the following during timing:
00056   // (Because they are slow, not because anything will break).
00057   double getTotalTime() const;
00058   static void consolidateAllTo(ostream &out=cout);
00059   void consolidateTo(ostream &out=cout) const;
00060   static void printAllTo(ostream &out=cout);
00061   void printTo(ostream &out=cout) const;
00062 
00063 public:
00064   /*
00065       e.g.
00066       void Foo() {
00067         ProfileTimer::AutoTimer(myProfiler, "Foo()");
00068         ...
00069       }
00070   */
00071   class EXPCL_PANDAEXPRESS AutoTimer {
00072   public:
00073     AutoTimer(ProfileTimer& profile, const char* tag);
00074     ~AutoTimer();
00075 
00076   protected:
00077     ProfileTimer& _profile;
00078     const char* _tag;
00079   };
00080 
00081 protected:
00082   static ProfileTimer* _head;
00083   ProfileTimer* _next;
00084   class TimerEntry {
00085   public:
00086     const char* _tag; // not owned by this.
00087     double _time;
00088   };
00089   double _on;
00090   double _elapsedTime;
00091   const char* _name; // not owned by this.
00092   int _maxEntries;
00093   int _entryCount;
00094   TimerEntry* _entries;
00095   int _autoTimerCount; // see class AutoTimer
00096 
00097   double getTime();
00098 
00099   friend class ProfileTimer::AutoTimer;
00100 };
00101 
00102 #include "profileTimer.I"
00103 
00104 #endif //]
 All Classes Functions Variables Enumerations