You can actually calculate the moving average in constant time with a ring buffer, such that you don't have to iterate over the whole buffer to compute it each time a new element is added. Just store the sum & size with the buffer. As an element is overwritten, subtract it from the sum and add its replacement. The average is just the sum / size.<p>It's a handy trick in financial software, where you often need to calculate relatively small sliding windows over enormously large data streams and latency is critical. The same technique generalizes to any computation that can be expressed as a fold function where the operation is invertible.