]> git.cworth.org Git - vogl/blob - src/voglcore/vogl_timer.h
Initial vogl checkin
[vogl] / src / voglcore / vogl_timer.h
1 /**************************************************************************
2  *
3  * Copyright 2013-2014 RAD Game Tools and Valve Software
4  * Copyright 2010-2014 Rich Geldreich and Tenacious Software LLC
5  * All Rights Reserved.
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a copy
8  * of this software and associated documentation files (the "Software"), to deal
9  * in the Software without restriction, including without limitation the rights
10  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11  * copies of the Software, and to permit persons to whom the Software is
12  * furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in
15  * all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23  * THE SOFTWARE.
24  *
25  **************************************************************************/
26
27 // File: vogl_timer.h
28 #pragma once
29
30 #include "vogl_core.h"
31
32 namespace vogl
33 {
34     typedef uint64_t timer_ticks;
35
36     class timer
37     {
38     public:
39         timer();
40         timer(timer_ticks start_ticks);
41
42         void start();
43         void start(timer_ticks start_ticks);
44
45         void stop();
46
47         double get_elapsed_secs() const;
48         inline double get_elapsed_ms() const
49         {
50             return get_elapsed_secs() * 1000.0f;
51         }
52         timer_ticks get_elapsed_us() const;
53         timer_ticks get_elapsed_ticks() const;
54
55         static void init();
56         static inline timer_ticks get_ticks_per_sec()
57         {
58             return g_freq;
59         }
60         static timer_ticks get_init_ticks();
61         static timer_ticks get_ticks();
62         static double ticks_to_secs(timer_ticks ticks);
63         static inline double ticks_to_ms(timer_ticks ticks)
64         {
65             return ticks_to_secs(ticks) * 1000.0f;
66         }
67         static inline double get_secs()
68         {
69             return ticks_to_secs(get_ticks());
70         }
71         static inline double get_ms()
72         {
73             return ticks_to_ms(get_ticks());
74         }
75
76     private:
77         static timer_ticks g_init_ticks;
78         static timer_ticks g_freq;
79         static double g_inv_freq;
80
81         timer_ticks m_start_time;
82         timer_ticks m_stop_time;
83
84         bool m_started : 1;
85         bool m_stopped : 1;
86     };
87
88     // Prints object's lifetime to stdout
89     class timed_scope
90     {
91         timer m_tm;
92         fixed_string256 m_name;
93
94     public:
95         inline timed_scope(const char *pName = "timed_scope")
96             : m_name(pName)
97         {
98             m_tm.start();
99         }
100
101         inline double get_elapsed_secs() const
102         {
103             return m_tm.get_elapsed_secs();
104         }
105         inline double get_elapsed_ms() const
106         {
107             return m_tm.get_elapsed_ms();
108         }
109
110         const timer &get_timer() const
111         {
112             return m_tm;
113         }
114         timer &get_timer()
115         {
116             return m_tm;
117         }
118
119         inline ~timed_scope()
120         {
121             double secs = m_tm.get_elapsed_secs();
122             printf("%s: %f secs, %f ms\n", m_name.get_ptr(), secs, secs * 1000.0f);
123         }
124     };
125
126 } // namespace vogl