_count = n if count > n else count
time_history = time_history[-_count:]
time_weights = list(range(1,len(time_history))) #just a simple linear weights
time_diff = [(i-j)*k for i,j in zip(time_history[1:], time_history[:-1],time_weights)]
speed = blockSize*(sum(time_weights)) / sum(time_diff)
为了使其更稳定,并且在下载高峰时不会做出反应,您还可以添加以下内容:
_count = n if count > n else count
time_history = time_history[-_count:]
time_history.remove(min(time_history))
time_history.remove(max(time_history))
time_weights = list(range(1, len(time_history))) #just a simple linear weights
time_diff = [(i-j)*k for i,j in zip(time_history[1:], time_history[:-1],time_weights)]
speed = blockSize*(sum(time_weights)) / sum(time_diff)
这将移除中的最高和最低峰值
time_history
这将使数字显示更加稳定。如果您想挑剔,可以在移除之前生成权重,然后使用
time_diff.index(min(time_diff))
.
也使用非线性函数(如
sqrt()
)权重生成会给你更好的结果。哦,正如我在评论中所说:在过滤时间中添加统计方法应该会稍微好一些,但我怀疑它不值得增加开销。