OpenTTD Source  1.11.0-beta2
pf_performance_timer.hpp
Go to the documentation of this file.
1 /*
2  * This file is part of OpenTTD.
3  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6  */
7 
10 #ifndef PF_PERFORMANCE_TIMER_HPP
11 #define PF_PERFORMANCE_TIMER_HPP
12 
13 #include "../debug.h"
14 
16 {
17  int64 m_start;
18  int64 m_acc;
19 
20  CPerformanceTimer() : m_start(0), m_acc(0) {}
21 
22  inline void Start()
23  {
24  m_start = QueryTime();
25  }
26 
27  inline void Stop()
28  {
29  m_acc += QueryTime() - m_start;
30  }
31 
32  inline int Get(int64 coef)
33  {
34  return (int)(m_acc * coef / QueryFrequency());
35  }
36 
37  inline int64 QueryTime()
38  {
39  return ottd_rdtsc();
40  }
41 
42  inline int64 QueryFrequency()
43  {
44  return ((int64)2200 * 1000000);
45  }
46 };
47 
49 {
50  CPerformanceTimer *m_pperf;
51 
52  inline CPerfStartReal(CPerformanceTimer& perf) : m_pperf(&perf)
53  {
54  if (m_pperf != nullptr) m_pperf->Start();
55  }
56 
57  inline ~CPerfStartReal()
58  {
59  Stop();
60  }
61 
62  inline void Stop()
63  {
64  if (m_pperf != nullptr) {
65  m_pperf->Stop();
66  m_pperf = nullptr;
67  }
68  }
69 };
70 
72 {
73  inline CPerfStartFake(CPerformanceTimer& perf) {}
74  inline ~CPerfStartFake() {}
75  inline void Stop() {}
76 };
77 
79 
80 #endif /* PF_PERFORMANCE_TIMER_HPP */
CPerfStartReal
Definition: pf_performance_timer.hpp:48
CPerfStartFake
Definition: pf_performance_timer.hpp:71
CPerformanceTimer
Definition: pf_performance_timer.hpp:15
ottd_rdtsc
uint64 ottd_rdtsc()
Get the tick counter from the CPU (high precision timing).
Definition: cpu.cpp:86