comparison base/Profiler.h @ 183:146eb9e35baa

* Improve output from Profiler class and make it incur less (no) overhead in release builds with NO_TIMING defined * Fix a lock contention issue in spectrogram * Marginal optimisations elsewhere
author Chris Cannam
date Tue, 10 Oct 2006 14:51:17 +0000
parents 4b2ea82fd0ed
children 91fdc752e540
comparison
equal deleted inserted replaced
182:f75f8a1cd7b1 183:146eb9e35baa
28 #include <sys/time.h> 28 #include <sys/time.h>
29 #include <map> 29 #include <map>
30 30
31 #include "RealTime.h" 31 #include "RealTime.h"
32 32
33 //#define NO_TIMING 1
34
35 #define WANT_TIMING 1
36
37 #ifdef NDEBUG
38 #ifndef WANT_TIMING
39 #define NO_TIMING 1
40 #endif
41 #endif
33 42
34 /** 43 /**
35 * Profiling classes 44 * Profiling classes
36 */ 45 */
37 46
45 public: 54 public:
46 static Profiles* getInstance(); 55 static Profiles* getInstance();
47 ~Profiles(); 56 ~Profiles();
48 57
49 void accumulate(const char* id, clock_t time, RealTime rt); 58 void accumulate(const char* id, clock_t time, RealTime rt);
50 void dump(); 59 void dump() const;
51 60
52 protected: 61 protected:
53 Profiles(); 62 Profiles();
54 63
55 typedef std::pair<clock_t, RealTime> TimePair; 64 typedef std::pair<clock_t, RealTime> TimePair;
56 typedef std::pair<int, TimePair> ProfilePair; 65 typedef std::pair<int, TimePair> ProfilePair;
57 typedef std::map<const char *, ProfilePair> ProfileMap; 66 typedef std::map<const char *, ProfilePair> ProfileMap;
58 typedef std::map<const char *, TimePair> LastCallMap; 67 typedef std::map<const char *, TimePair> LastCallMap;
68 typedef std::map<const char *, TimePair> WorstCallMap;
59 ProfileMap m_profiles; 69 ProfileMap m_profiles;
60 LastCallMap m_lastCalls; 70 LastCallMap m_lastCalls;
71 WorstCallMap m_worstCalls;
61 72
62 static Profiles* m_instance; 73 static Profiles* m_instance;
63 }; 74 };
64 75
76 #ifndef NO_TIMING
77
78 /**
79 * Profile point instance class. Construct one of these on the stack
80 * at the start of a function, in order to record the time consumed
81 * within that function. The profiler object should be effectively
82 * optimised out if NO_TIMING is defined, so any overhead in a release
83 * build should be negligible so long as you remember to define that.
84 */
65 class Profiler 85 class Profiler
66 { 86 {
67 public: 87 public:
68 Profiler(const char*, bool showOnDestruct = false); 88 /**
89 * Create a profile point instance that records time consumed
90 * against the given profiling point name. If showOnDestruct is
91 * true, the time consumed will be printed to stderr when the
92 * object is destroyed; otherwise, only the accumulated, mean and
93 * worst-case times will be shown when the program exits or
94 * Profiles::dump() is called.
95 */
96 Profiler(const char *name, bool showOnDestruct = false);
69 ~Profiler(); 97 ~Profiler();
70 98
71 void update(); 99 void update() const;
72 100
73 protected: 101 protected:
74 const char* m_c; 102 const char* m_c;
75 clock_t m_startCPU; 103 clock_t m_startCPU;
76 RealTime m_startTime; 104 RealTime m_startTime;
77 bool m_showOnDestruct; 105 bool m_showOnDestruct;
78 }; 106 };
79 107
108 #else
109
110 class Profiler
111 {
112 public:
113 Profiler(const char *, bool) { }
114 ~Profiler() { }
115
116 void update() { }
117 };
80 118
81 #endif 119 #endif
120
121 #endif