diff options
author | Yuri Gorshenin <y@maps.me> | 2016-09-27 19:20:43 +0300 |
---|---|---|
committer | Yuri Gorshenin <y@maps.me> | 2016-09-28 14:46:25 +0300 |
commit | c9ef1fa3663816af59c09311d0004a0bb546c236 (patch) | |
tree | 9a7b4613b8239ceb42ea3c0daefc2689b7cbdb0f /base | |
parent | 5951f4c0f834a139a78aa375e839b84ebde75bd4 (diff) |
Review fixes.
Diffstat (limited to 'base')
-rw-r--r-- | base/time_samples.cpp | 14 | ||||
-rw-r--r-- | base/time_samples.hpp | 22 |
2 files changed, 25 insertions, 11 deletions
diff --git a/base/time_samples.cpp b/base/time_samples.cpp index 3a0d439e18..209385b060 100644 --- a/base/time_samples.cpp +++ b/base/time_samples.cpp @@ -4,21 +4,21 @@ namespace my { -void TimeSamples::Add(double s) +void TimeSamples::Add(double seconds) { - m_s += s; - m_s2 += s * s; + m_sum += seconds; + m_sum2 += seconds * seconds; ++m_total; } -double TimeSamples::GetMean() const { return m_total == 0 ? 0.0 : m_s / m_total; } +double TimeSamples::GetMean() const { return m_total == 0 ? 0.0 : m_sum / m_total; } -double TimeSamples::GetSd() const +double TimeSamples::GetSD() const { if (m_total < 2) return 0.0; - return std::max((m_s2 - m_s * m_s / static_cast<double>(m_total)) / (m_total - 1), 0.0); + return std::max((m_sum2 - m_sum * m_sum / static_cast<double>(m_total)) / (m_total - 1), 0.0); } -double TimeSamples::GetVar() const { return sqrt(GetSd()); } +double TimeSamples::GetVar() const { return sqrt(GetSD()); } } // namespace my diff --git a/base/time_samples.hpp b/base/time_samples.hpp index b37166b164..c4f1fdf072 100644 --- a/base/time_samples.hpp +++ b/base/time_samples.hpp @@ -6,21 +6,35 @@ namespace my { +// This class can be used in measurements of code blocks performance. +// It can accumulate time samples, and can calculate mean time and +// standard deviation. +// +// *NOTE* This class is NOT thread-safe. class TimeSamples final { public: - void Add(double s); + void Add(double seconds); + // Mean of the accumulated time samples. double GetMean() const; - double GetSd() const; + + // Unbiased standard deviation of the accumulated time samples. + double GetSD() const; + + // Unbiased variance of the accumulated time samples. double GetVar() const; private: - double m_s = 0.0; - double m_s2 = 0.0; + double m_sum = 0.0; + double m_sum2 = 0.0; size_t m_total = 0; }; +// This class can be used as a single scoped time sample. It +// automatically adds time sample on destruction. +// +// *NOTE* This class is NOT thread-safe. class ScopedTimeSample final { public: |