make the sliding average class a template

This commit is contained in:
Arvid Norberg 2010-04-16 16:06:17 +00:00
parent 50c334660d
commit f092ddb49e
2 changed files with 6 additions and 4 deletions

View File

@ -422,10 +422,10 @@ namespace libtorrent
cache_status m_cache_stats; cache_status m_cache_stats;
// keeps average queue time for disk jobs (in microseconds) // keeps average queue time for disk jobs (in microseconds)
sliding_average m_queue_time; sliding_average<512> m_queue_time;
// average read time for cache misses (in microseconds) // average read time for cache misses (in microseconds)
sliding_average m_read_time; sliding_average<512> m_read_time;
#ifdef TORRENT_DISK_STATS #ifdef TORRENT_DISK_STATS
std::ofstream m_log; std::ofstream m_log;

View File

@ -35,6 +35,7 @@ namespace libtorrent
// a sliding average accumulator. Add samples to it and it // a sliding average accumulator. Add samples to it and it
// keeps track of a sliding mean value and an average deviation // keeps track of a sliding mean value and an average deviation
// from that average. // from that average.
template <int history_size>
struct sliding_average struct sliding_average
{ {
sliding_average(): m_mean(-1), m_average_deviation(-1) {} sliding_average(): m_mean(-1), m_average_deviation(-1) {}
@ -48,14 +49,15 @@ struct sliding_average
} }
int deviation = abs(m_mean - s); int deviation = abs(m_mean - s);
m_mean = m_mean - m_mean / 16 + s / 16; m_mean = m_mean - m_mean / history_size + s / history_size;
if (m_average_deviation == -1) if (m_average_deviation == -1)
{ {
m_average_deviation = deviation; m_average_deviation = deviation;
return; return;
} }
m_average_deviation = m_average_deviation - m_average_deviation / 16 + deviation / 16; m_average_deviation = m_average_deviation - m_average_deviation
/ history_size + deviation / history_size;
} }
int mean() const { return m_mean != -1 ? m_mean : 0; } int mean() const { return m_mean != -1 ? m_mean : 0; }