最近的平均值,没有完整记录所有数据
这主要是一个性能问题。它是为了帮助获得过去N(30?)个值的当前平均值。这里的上下文是监视服务器中过去 N 个数据请求的平均执行时间。而显而易见的解决方案是记录所有过去的 N 个请求,其中读取所有 N 个请求并将其制成表格。造成与相关数据请求相关的性能损失。
由于这主要用作测量手段,而不是完美的运行估计,所以问题是什么?以最有效的方式解决这个问题。
虽然解决方案可能是语言中立的,但我将在 php 中实现:)
This is mainly a performance issue. It is to help get the current average of the past N (30?) values. The context here would be to monitor the average execution time for the past N data requests in a server. While the obvious solution, is to log the all past N requests, in which all N request is read, and tabulated. Creating a performance hit relative to the data request in question.
As this is mainly use as a means of measure and not a perfect running estimate, the question? To solve this problem in the most effective manner.
While the solution may be language neutral, I would be implementing in php :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不要保存最后 N 个值,而是使用预定义的存储桶大小将值组织在“存储桶”中,并且对于每个存储桶,仅保存存储桶中所有值的总和。
当存储桶已满时,您可以删除最旧的存储桶。 (最好的方法是使用环形缓冲区)
这允许您将内存使用量减少 BUCKET_SIZE 倍,但显然您的平均值不再超过最后 N 个值,而是超过最后 N 到 N + BUCKET_SIZE。
Instead of saving the last N values, organize your values in "buckets" with a predefined bucket-size, and for each bucket, only save the sum of all values in the bucket.
Whenever a bucket is full, you can delete the oldest bucket. (The best way to do this would be a ring buffer)
This allows you to cut your memory usage by a factor of BUCKET_SIZE, but obviously your average is not over the last N values anymore, but over the last N to N + BUCKET_SIZE.