forked from ArcheGraphics/HydraViewer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
yangfengzzz
committed
Jan 15, 2024
1 parent
f0bdebc
commit 6df15b0
Showing
4 changed files
with
102 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// Copyright (c) 2024 Feng Yang | ||
// | ||
// I am making my contributions/submissions to this project solely in my | ||
// personal capacity and am not conveying any rights to any intellectual | ||
// property of any third parties. | ||
|
||
#include "framerate.h" | ||
|
||
namespace vox { | ||
|
||
Framerate::Framerate(size_t n) noexcept | ||
: _history_size{n} { | ||
_durations.reserve(n); | ||
_frames.reserve(n); | ||
_last = Clock::now(); | ||
} | ||
|
||
void Framerate::clear() noexcept { | ||
_durations.clear(); | ||
_frames.clear(); | ||
_last = Clock::now(); | ||
} | ||
|
||
double Framerate::duration() const noexcept { | ||
auto dt = Clock::now() - _last; | ||
using namespace std::chrono_literals; | ||
return static_cast<double>(dt / 1ns) * 1e-9; | ||
} | ||
|
||
void Framerate::record(size_t frame_count) noexcept { | ||
if (_durations.size() == _history_size) { | ||
_durations.erase(_durations.begin()); | ||
_frames.erase(_frames.begin()); | ||
} | ||
using namespace std::chrono_literals; | ||
_durations.emplace_back(duration()); | ||
_frames.emplace_back(frame_count); | ||
_last = Clock::now(); | ||
} | ||
|
||
double Framerate::report() const noexcept { | ||
if (_durations.empty()) { return 0.0; } | ||
auto total_duration = 0.0; | ||
auto total_frame_count = static_cast<size_t>(0u); | ||
for (auto i = 0u; i < _durations.size(); i++) { | ||
total_duration += _durations[i]; | ||
total_frame_count += _frames[i]; | ||
} | ||
return static_cast<double>(total_frame_count) / total_duration; | ||
} | ||
|
||
}// namespace vox |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Copyright (c) 2024 Feng Yang | ||
// | ||
// I am making my contributions/submissions to this project solely in my | ||
// personal capacity and am not conveying any rights to any intellectual | ||
// property of any third parties. | ||
|
||
#pragma once | ||
|
||
#include <chrono> | ||
|
||
#include <vector> | ||
|
||
namespace vox { | ||
|
||
class Framerate { | ||
|
||
public: | ||
using Clock = std::chrono::steady_clock; | ||
using Timepoint = Clock::time_point; | ||
|
||
private: | ||
std::vector<double> _durations; | ||
std::vector<size_t> _frames; | ||
Timepoint _last; | ||
size_t _history_size; | ||
|
||
public: | ||
explicit Framerate(size_t n = 5) noexcept; | ||
void clear() noexcept; | ||
void record(size_t frame_count = 1u) noexcept; | ||
[[nodiscard]] double duration() const noexcept; | ||
[[nodiscard]] double report() const noexcept; | ||
}; | ||
|
||
}// namespace vox |