Timing
Timer
-
class Timer
Class that measures times between tick points.
Subclassed by obscura::FrameProfiler< std::chrono::seconds >, obscura::FrameProfiler< HorizonTimeUnit >
Public Functions
-
Timer() = default
-
void start()
Starts the timer. Must be called before measuring any times. Otherwise, an exception is thrown.
-
void tick()
Creates a tick point that is used to measure intermediate timings.
-
void stop()
Stops the timer. The timing between the start and stop point can be gathered afterward.
-
auto getStart() const -> TimePoint
-
auto getTick() const -> TimePoint
-
auto getEnd() const -> TimePoint
-
auto isStarted() const -> bool
-
auto isStopped() const -> bool
-
template<class TimeUnit = TimePoint::duration>
inline auto getDuration() const -> TimeUnit Returns the duration between the stop and start point, if stop was called, or otherwise the duration between the current time point and the start point.
- Template Parameters:
TimeUnit – Type of the duration to return. Usually, std::chrono::duration.
- Returns:
Returns the duration between stop/current point and start point.
-
template<class TimeUnit = TimePoint::duration>
inline auto getDurationSinceLastTick() -> TimeUnit Returns the duration between the stop and last tick point, if stop was called, or otherwise the duration between the current time point and the last tick point.
- Template Parameters:
TimeUnit – Type of the duration to return. Usually, std::chrono::duration.
- Returns:
Returns the duration between stop/current point and last tick point.
-
Timer() = default
FrameProfiler
-
template<duration HorizonTimeUnit = std::chrono::seconds>
class FrameProfiler : public obscura::Timer Class that profiles frame times. It can be used to receive the FPS or intervals between frames. It is an extension of obscura::Timer.
- Template Parameters:
HorizonTimeUnit – Type of the time horizon for the FPS averaging. In order to measure the FPS it is necessary to provide a time window in which the frame times are averaged. This template parameter specifies the type of that duration.
Public Functions
-
inline explicit FrameProfiler(const HorizonTimeUnit &timePointsHorizon = std::chrono::seconds{2})
The constructor requires the time horizon for the FPS aggregation. Since the FPS measures how many frames there are per second, it must be specified over what time horizon the frames will be counted. A longer time horizon will be less sensible to outliers while a shorter horizon will be make frame time spikes more visible.
- Parameters:
timePointsHorizon – Time horizon for FPS counting. Default: 2 seconds
-
inline void start()
Must be called before measuring any frame times.
-
inline void tick()
Should be called at the beginning of each frame.
-
inline void stop()
Stops the measurement. Calling this function is not required for cleaning up the frame profiler.
-
template<duration TimeUnit>
inline auto getFPS(const TimeUnit &aggregationTimeframe) -> FPS Returns FPS measured over the given aggregation horizon. The given time frame cannot be longer than the specified time horizon in the constructor.
- Template Parameters:
TimeUnit – The time unit for the aggregation time frame.
- Parameters:
aggregationTimeframe – The aggregation time frame.
- Returns:
-
inline auto getDelta() const -> float
- Returns:
Frame time delta in seconds.
-
inline void setReferenceTimePoint()
Creates a reference point from where frame time computations are carried out.
This function should be called at the beginning of any frame time computations in order to guarantee the consistency of consecutive calls to aggregating function. All aggregate computations concerning frame times must have a rolling sample window. That sample window is carried out from either this measuring point or from the current time point. If a reference point is not set the sample window is changing with consecutive calls.
Note that you also have to set createNewReferencePoint to false in any function that requires reference points in order to use this reference point.
Example: tick - tick - sample window start - tick - tick - setReferenceTimePoint point/sample window end
-
inline auto getNumberOfFrameTimes() const -> std::size_t
Returns the number of frames within the time point horizon.
The time point horizon is set in the constructor. A frame is considered within the horizon if it intersects with the horizon.
- Returns:
Number of frames.
-
template<duration TimeUnit>
inline auto getNumberOfFrameTimes(const TimeUnit &withinDuration, bool createNewReferencePoint = true) -> std::size_t Returns the number of frames within the given duration.
A frame is considered within the duration if it intersects with it.
- Template Parameters:
TimeUnit – The time unit for the duration.
- Parameters:
withinDuration – The duration in which frames should be counted.
- Returns:
Number of frames.
-
inline auto getFrameTimes() const -> std::list<TimePoint::duration>
Returns the frame times within the time point horizon.
The time point horizon is set in the constructor. A frame is considered within the horizon if it intersects with the horizon.
- Returns:
List of frame times within the horizon.
-
template<duration FrameTimeUnit, duration CutOffTimeUnit>
inline auto getFrameTimes(const CutOffTimeUnit &withinDuration, bool createNewReferencePoint = true) -> std::list<FrameTimeUnit> Returns the frame times within the given duration.
A frame is considered within the duration if it intersects with it.
- Template Parameters:
TimeUnit – The time unit for the returned frame times.
CutOffTimeUnit – The time unit for the time window.
- Parameters:
withinDuration – The duration in which frames should be counted.
createNewReferencePoint – Whether or not to create a new reference point for the time window. Create a manual reference point if you consecutively call functions with a time window in order to keep a consistent time window.
- Returns:
List of frame times within the given duration.
-
template<duration TimeUnit = TimePoint::duration>
inline auto getCumulativeFrameTimes() const -> TimeUnit Returns the summed up frame times within the time point horizon.
- Template Parameters:
TimeUnit – The time unit for the duration.
- Returns:
Sum of frame times within the horizon.
-
template<duration CutOffTimeUnit, duration TimeUnit = TimePoint::duration>
inline auto getCumulativeFrameTimes(const CutOffTimeUnit &withinDuration, bool createNewReferencePoint = true) -> TimeUnit Returns the summed up frame times within the given duration.
- Template Parameters:
CutOffTimeUnit – The time unit for the time window.
TimeUnit – The time unit for the returned frame times.
- Parameters:
withinDuration – The duration in which frames should be counted.
createNewReferencePoint – Whether or not to create a new reference point for the time window. Create a manual reference point if you consecutively call functions with a time window in order to keep a consistent time window.
- Returns:
Sum of frame times within the given duration.
-
template<duration TimeUnit = TimePoint::duration>
inline auto getDurationSinceLastTick(bool createNewReferencePoint = true) -> TimeUnit Nearly the same as obscura::Timer::getDurationSinceLastTick with the difference that a reference point can be used.
- Template Parameters:
TimeUnit – Type of duration to return.
- Parameters:
createNewReferencePoint – Whether or not to create a new reference point. Set this to false if you have manually created a reference point.
- Returns:
The duration from end/reference point to the last tick.
FramePacer
-
template<duration HorizonTimeUnit = std::chrono::seconds>
class FramePacer : public obscura::FrameProfiler<std::chrono::seconds> Responsible for pacing frames and reaching consisting frame times.
- Template Parameters:
HorizonTimeUnit – See obscura::FrameProfiler for more information.