我如何估算瞬时吞吐量?例如,以类似于下载文件时浏览器的方式。它不仅仅是平均吞吐量,而是瞬时估计,可能是“移动平均值”。我正在寻找算法,但您可以在c ++中指定它。理想情况下,它不涉及线程(即,连续刷新,例如每秒),而是仅在询问值时进行评估。
答案 0 :(得分:3)
您可以使用指数移动平均线,如here所述,但我会重复以下公式:
accumulator = (alpha * new_value) + (1.0 - alpha) * accumulator
要实现估算,假设您打算每秒查询一次计算,但您希望在最后一分钟内获得平均值。然后,这将是获得该估计的一种方法:
struct AvgBps {
double rate_; // The average rate
double last_; // Accumulates bytes added until average is computed
time_t prev_; // Time of previous update
AvgBps () : rate_(0), last_(0), prev_(time(0)) {}
void add (unsigned bytes) {
time_t now = time(0);
if (now - prev_ < 60) { // The update is within the last minute
last_ += bytes; // Accumulate bytes into last
if (now > prev_) { // More than a second elapsed from previous
// exponential moving average
// the more time that has elapsed between updates, the more
// weight is assigned for the accumulated bytes
double alpha = (now - prev_)/60.0;
rate = alpha * last_ + (1 - alpha) * rate;
last_ = 0; // Reset last_ (it has been averaged in)
prev_ = now; // Update prev_ to current time
}
} else { // The update is longer than a minute ago
rate_ = bytes; // Current update is average rate
last_ = 0; // Reset last_
prev_ = now; // Update prev_
}
}
double rate () {
add(0); // Compute rate by doing an update of 0 bytes
return rate_; // Return computed rate
}
};
您实际上应该使用单调时钟而不是time
。
答案 1 :(得分:0)
你可能想要一辆平均车厢。
只保留最后n个值,并对它们取平均值。对于每个后续块,减去最旧的块并添加最新块。请注意,对于浮点值,您可能会收到一些聚合错误,在这种情况下,您可能希望每m个值从头开始重新计算总计。当然,对于整数值,您不需要这样的东西。