I have an integer property which is updated every second with a signal-strength value ranging from 0 – 100.
I’d like to be able to keep an ongoing measure of the moving average over the last 10, 25, 50 measurements.
What’s the most efficient way of doing this?
I’m currently thinking of implementing a set of FIFO queues using NSMutableArray and popping the leading value every time I add a new one at the end once the array has the requisite number of entries. However, I’m not sure if there’s a more efficient way of doing this or not.
A queue is the right way. The real efficiency comes with how you recalculate the average.
It should be done with:
i.e. the new running average is simply the old average minus the weight of the oldest value you dropped, plus the weight of the newest value you enqueued.