php - Recent average, without full logging of all data

532

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 :)

462

Answer

Solution:

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.

People are also looking for solutions to the problem: PHP Based PDF Modification Framework / Package

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.