source: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Timer/PerfTimerSkeleton.h @ 2777

Revision 2777, 4.7 KB checked in by mattausch, 17 years ago (diff)
Line 
1// ---------------------------------------------------------------------------
2//  class PERFTIMER definition
3// ---------------------------------------------------------------------------
4
5#ifndef DUMMY_TIMER
6
7/** Very accurate Timer class for benchmarking on SGI systems.
8        This timer can be used to accurately take measurements
9        on all SGI-systems, usually in the microseconds range.
10
11        Note: getting the time on an unavailable Timer returns garbage.
12*/
13class PERFTIMER
14{
15private:
16        static TIMEVAL_T frequency;
17        static bool isinitialized;
18        static bool available;
19
20public: // public for macro versions of Entry/Exit
21        TIMEVAL_T start_count;
22        TIMEVAL_T total_count;
23
24
25public:
26       
27        // this retrieves the timer frequency - will be called by constructor
28        // NOTE: allow this to be called externally, so logging can be enabled!
29        static void InitClass(bool verbose);
30
31       
32        /// the time type used for Perftimers
33        typedef double t_time;
34
35        /// returns the frequency
36        static double GetFrequency(bool verbose = false);
37
38        /// Constructor calls Start
39        PERFTIMER(bool verbose = false);
40
41        /// use in conjunction with Exit() to time parts of code that are called more than once
42        void Entry();
43        /// stop counting but remember total
44        void Exit();
45        /// read total time elapsed and reset the time - to be used with Entry()/Exit()
46        double TotalTime();
47       
48        /// reset and start the counter - use for timing one piece of code (implicit Entry())
49        void Start();
50        /** returns elapsed time in seconds - if restart is true, timer is also restarted - use with Start()
51                (implicit Exit()/Entry() with same timestamp, with restart: implicit Exit()/TotalTime()/Entry())
52        */
53        double Elapsed(bool restart = true);
54        /** returns elapsed time in milliseconds - if restart is true, timer is also restarted - use with Start()
55                (implicit Exit()/Entry() with same timestamp, with restart: implicit Exit()/TotalTime()/Entry())
56        */
57        double Elapsedms(bool restart = true);
58       
59        // returns elapsed time in cycles - if restart is true, timer is also restarted
60        //unsigned int ElapsedCycles(bool restart = true);
61       
62        /// false if no appropriate hardware found
63        bool Available();
64};
65
66// ---------------------------------------------------------------------------
67//  class PERFTIMER inline implementation
68// ---------------------------------------------------------------------------
69
70PERFTIMER_INLINE bool PERFTIMER::Available()
71{
72        /// HACK: no SGI detection!
73        return available;
74}
75
76// macro version to ensure inlining
77#define PERF_ENTRY(perftimer) TIMEVAL_GET((perftimer).start_count);
78
79PERFTIMER_INLINE void PERFTIMER::Entry()
80{
81        TIMEVAL_GET(start_count);
82}
83
84PERFTIMER_INLINE void PERFTIMER::Start()
85{
86        TIMEVAL_ZERO(total_count);
87        Entry();
88}
89
90// macro version to ensure inlining
91#define PERF_EXIT(perftimer) { TIMEVAL_T end_count; TIMEVAL_GET(end_count); TIMEVAL_ADD((perftimer).total_count, end_count, (perftimer).start_count);}
92
93PERFTIMER_INLINE void PERFTIMER::Exit()
94{
95        TIMEVAL_T end_count;
96        TIMEVAL_GET(end_count);
97        TIMEVAL_ADD(total_count, end_count, start_count);
98}
99
100
101PERFTIMER_INLINE double PERFTIMER::TotalTime()
102{
103        double time = TIMEVAL_TODOUBLE(total_count);
104        TIMEVAL_ZERO(total_count);
105
106        return time;
107}
108
109// returns elapsed time in seconds - if restart is true, timer is also restarted
110PERFTIMER_INLINE double PERFTIMER::Elapsed(bool restart)
111{
112        TIMEVAL_T end_count;
113        TIMEVAL_GET(end_count);
114        TIMEVAL_ADD(total_count, end_count, start_count);
115
116        TIMEVAL_ASSIGN(start_count, end_count); // unnecessary for entry/exit operation, but doesn't hurt
117
118        double time = TIMEVAL_TODOUBLE(total_count);
119
120        if (restart)
121        {
122                TIMEVAL_ZERO(total_count);
123        }
124       
125        return time;
126}
127
128// returns elapsed time in milliseconds - if restart is true, timer is also restarted
129PERFTIMER_INLINE double PERFTIMER::Elapsedms(bool restart)
130{
131        TIMEVAL_T end_count;
132        TIMEVAL_GET(end_count);
133        TIMEVAL_ADD(total_count, end_count, start_count);
134
135        TIMEVAL_ASSIGN(start_count, end_count); // unnecessary for entry/exit operation, but doesn't hurt
136
137        double time = TIMEVAL_TODOUBLE(total_count);
138
139        if (restart)
140        {
141                TIMEVAL_ZERO(total_count);
142        }
143       
144        return time * 1000.;
145}
146
147// Constructor calls Start
148PERFTIMER_INLINE PERFTIMER::PERFTIMER(bool verbose)
149{
150        if (!isinitialized)
151                InitClass(verbose);
152
153        //TIMEVAL_ZERO(total_count);
154        Start();
155}
156
157#else
158
159class PERFTIMER
160{
161public:
162        PERFTIMER(bool x = false) {}
163        static double GetFrequency(bool) {return 1;}
164        void Start() {}
165        double Elapsed(bool restart = true) { return 0; }
166        double Elapsedms(bool restart = true) { return 0; }
167        void Entry() {}
168        void Exit() {}
169        double TotalCount() { return 0; }
170        bool Available() { return false; }
171};
172
173#endif
Note: See TracBrowser for help on using the repository browser.