Timer.h
Go to the documentation of this file.
1 // This file is a part of the OpenSurgSim project.
2 // Copyright 2013, SimQuest Solutions Inc.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 
16 #ifndef SURGSIM_FRAMEWORK_TIMER_H
17 #define SURGSIM_FRAMEWORK_TIMER_H
18 
19 #include <boost/chrono.hpp>
20 #include <deque>
21 
22 namespace SurgSim
23 {
24 namespace Framework
25 {
26 
29 class Timer
30 {
31 public:
33  Timer();
34 
36  void start();
37 
39  void beginFrame();
40 
44  void endFrame();
45 
47  void markFrame();
48 
51  double getCumulativeTime() const;
52 
55  double getAverageFramePeriod() const;
56 
59  double getAverageFrameRate() const;
60 
64  double getLastFramePeriod() const;
65 
68  double getLastFrameRate() const;
69 
71  void setMaxNumberOfFrames(size_t numberOfFrames);
72 
74  size_t getCurrentNumberOfFrames() const;
75 
78  size_t getNumberOfClockFails() const;
79 
81  double getMaxFramePeriod() const;
82 
84  double getMinFramePeriod() const;
85 
86 private:
88  typedef boost::chrono::steady_clock TimerClock;
89 
91  typedef boost::chrono::duration<double> TimerDuration;
92 
94  typedef boost::chrono::time_point<TimerClock, TimerDuration> TimerTimePoint;
95 
98  TimerTimePoint now();
99 
101  static const TimerClock m_clock;
102 
104  TimerTimePoint m_lastTime;
105 
108 
110  std::deque<TimerDuration> m_frameDurations;
111 
113  size_t m_clockFails;
114 };
115 
116 } // Framework
117 } // SurgSim
118 
119 #endif
Definition: DriveElementFromInputBehavior.cpp:27
double getCumulativeTime() const
Return the sum of the durations over all the stored frames.
Definition: Timer.cpp:58
double getLastFrameRate() const
Return the inverse of the duration of the most-recent frame.
Definition: Timer.cpp:84
Timer()
Instantiate a TimerClock and start a timing run.
Definition: Timer.cpp:24
size_t m_clockFails
Number of clock errors since last start.
Definition: Timer.h:113
size_t m_maxNumberOfFrames
Maximum number of frames to store.
Definition: Timer.h:107
std::deque< TimerDuration > m_frameDurations
Durations of the frames, i.e., the "stored frames".
Definition: Timer.h:110
boost::chrono::time_point< TimerClock, TimerDuration > TimerTimePoint
Time points used by the Timer class.
Definition: Timer.h:94
double getLastFramePeriod() const
Return the duration of the most-recent frame (time between last endFrame and the previous start...
Definition: Timer.cpp:77
size_t getNumberOfClockFails() const
Definition: Timer.cpp:104
boost::chrono::duration< double > TimerDuration
Durations used by the Timer class.
Definition: Timer.h:91
double getMaxFramePeriod() const
Definition: Timer.cpp:120
Timer class, measures execution times.
Definition: Timer.h:29
size_t getCurrentNumberOfFrames() const
Definition: Timer.cpp:99
void setMaxNumberOfFrames(size_t numberOfFrames)
Set the maximum number of frames to store.
Definition: Timer.cpp:89
void start()
Begin a timing run by clearing out any stored frames and beginning a frame.
Definition: Timer.cpp:30
void endFrame()
End this frame by storing the duration since the current frame was begun.
Definition: Timer.cpp:42
TimerTimePoint m_lastTime
The time at last start, beginFrame, or markFrame.
Definition: Timer.h:104
double getAverageFrameRate() const
Return the inverse of the average duration across all stored frames.
Definition: Timer.cpp:72
static const TimerClock m_clock
The clock used to get the time.
Definition: Timer.h:101
boost::chrono::steady_clock TimerClock
The Clock used by the Timer class.
Definition: Timer.h:88
double getAverageFramePeriod() const
Return the average duration across all stored frames.
Definition: Timer.cpp:67
TimerTimePoint now()
Get the current time.
Definition: Timer.cpp:109
void markFrame()
End the current frame and begin a new frame.
Definition: Timer.cpp:52
void beginFrame()
Begin a frame (storing the current time).
Definition: Timer.cpp:37
double getMinFramePeriod() const
Definition: Timer.cpp:127