diff options
author | mpimenov <mpimenov@users.noreply.github.com> | 2016-09-28 17:39:28 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-09-28 17:39:28 +0300 |
commit | 2143577410e715b5867addfb97bb0fdc2c18b170 (patch) | |
tree | a897df805213b862e0af9cffbe691f61f2bcd7a9 /base | |
parent | a7575ec0e3711220ea49a0786c69e705743d4118 (diff) | |
parent | c9ef1fa3663816af59c09311d0004a0bb546c236 (diff) |
Merge pull request #4390 from ygorshenin/fix-locality-finder
[search] Fixed locality matching code.
Diffstat (limited to 'base')
-rw-r--r-- | base/base.pro | 4 | ||||
-rw-r--r-- | base/time_samples.cpp | 24 | ||||
-rw-r--r-- | base/time_samples.hpp | 49 | ||||
-rw-r--r-- | base/timer.hpp | 2 |
4 files changed, 77 insertions, 2 deletions
diff --git a/base/base.pro b/base/base.pro index d0976ea2f7..803d1b544a 100644 --- a/base/base.pro +++ b/base/base.pro @@ -10,9 +10,9 @@ include($$ROOT_DIR/common.pri) SOURCES += \ base.cpp \ condition.cpp \ - gmtime.cpp \ deferred_task.cpp \ exception.cpp \ + gmtime.cpp \ internal/message.cpp \ logging.cpp \ lower_case.cpp \ @@ -28,6 +28,7 @@ SOURCES += \ thread_checker.cpp \ thread_pool.cpp \ threaded_container.cpp \ + time_samples.cpp \ timegm.cpp \ timer.cpp \ @@ -81,6 +82,7 @@ HEADERS += \ threaded_container.hpp \ threaded_list.hpp \ threaded_priority_queue.hpp \ + time_samples.hpp \ timegm.hpp \ timer.hpp \ worker_thread.hpp \ diff --git a/base/time_samples.cpp b/base/time_samples.cpp new file mode 100644 index 0000000000..209385b060 --- /dev/null +++ b/base/time_samples.cpp @@ -0,0 +1,24 @@ +#include "base/time_samples.hpp" + +#include "std/cmath.hpp" + +namespace my +{ +void TimeSamples::Add(double seconds) +{ + m_sum += seconds; + m_sum2 += seconds * seconds; + ++m_total; +} + +double TimeSamples::GetMean() const { return m_total == 0 ? 0.0 : m_sum / m_total; } + +double TimeSamples::GetSD() const +{ + if (m_total < 2) + return 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()); } +} // namespace my diff --git a/base/time_samples.hpp b/base/time_samples.hpp new file mode 100644 index 0000000000..c4f1fdf072 --- /dev/null +++ b/base/time_samples.hpp @@ -0,0 +1,49 @@ +#pragma once + +#include "base/timer.hpp" + +#include "std/cstdint.hpp" + +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 seconds); + + // Mean of the accumulated time samples. + double GetMean() 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_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: + ScopedTimeSample(TimeSamples & ts) : m_ts(ts) {} + ~ScopedTimeSample() { m_ts.Add(m_t.ElapsedSeconds()); } + +private: + TimeSamples & m_ts; + my::Timer m_t; +}; + +} // namespace my diff --git a/base/timer.hpp b/base/timer.hpp index bd793f92c1..d64c66a0ac 100644 --- a/base/timer.hpp +++ b/base/timer.hpp @@ -73,4 +73,4 @@ public: time_t SecondsSinceEpochToTimeT(uint64_t secondsSinceEpoch); uint64_t TimeTToSecondsSinceEpoch(time_t time); -} +} // namespace my |