简体   繁体   中英

Calculate running average

I have a stream of data containing brand and consumption data;and am required to provide a real-time average consumption data on demand.
This is a high-volume ,low latency tick.
Typical last 5 ticks may be :

P&G 345.21
J&J 124.9
P&G 127.9
WAL 789
KMR 78.5

The possible options :
Populate data in a array/list - aggregate and return average on demand(not feasible considering volume)
Populate map with key and aggregate data on tick.Maintain a separate map with kep and tick count.Calculate and return average from map 1 and 2 on demand.
Is there a better data structure/algorithm to achieve this?

For your issue, it is better to maintain an another map contains a dict of {brand: (num, mean)} , then when get new consumption x , you can update dict with

new_mean = mean + (x-mean)/(n+1)
n = n+1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM