diff options
author | Yuri Gorshenin <y@maps.me> | 2016-09-21 17:25:16 +0300 |
---|---|---|
committer | Yuri Gorshenin <y@maps.me> | 2016-09-27 16:29:47 +0300 |
commit | 5951f4c0f834a139a78aa375e839b84ebde75bd4 (patch) | |
tree | 55313679578a29fa47aa07078334377692189bc0 /base | |
parent | 87312eac6498904a0f33cf51fe3524aff83b61cc (diff) |
[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 | 35 | ||||
-rw-r--r-- | base/timer.hpp | 2 |
4 files changed, 63 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..3a0d439e18 --- /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 s) +{ + m_s += s; + m_s2 += s * s; + ++m_total; +} + +double TimeSamples::GetMean() const { return m_total == 0 ? 0.0 : m_s / m_total; } + +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); +} + +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..b37166b164 --- /dev/null +++ b/base/time_samples.hpp @@ -0,0 +1,35 @@ +#pragma once + +#include "base/timer.hpp" + +#include "std/cstdint.hpp" + +namespace my +{ +class TimeSamples final +{ +public: + void Add(double s); + + double GetMean() const; + double GetSd() const; + double GetVar() const; + +private: + double m_s = 0.0; + double m_s2 = 0.0; + size_t m_total = 0; +}; + +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 |