How to calculate simple moving average faster in C#?

后端 未结 14 2615
囚心锁ツ
囚心锁ツ 2020-12-15 06:42

What is the fastest library/algorithm for calculating simple moving average? I wrote my own, but it takes too long on 330 000 items decimal dataset.

  • period /
14条回答
  •  渐次进展
    2020-12-15 07:07

    If the data is static, you can preprocess the array to make moving average queries very fast:

    decimal[] GetCSum(decimal[] data) {
        decimal csum[] = new decimal[data.Length];
        decimal cursum = 0;
        for(int i=0; i

    Now the moving average calculation is easy and fast:

    decimal CSumMovingAverage(decimal[] csum, int period, int ii) {
        if(period == 0 || ii <= period)
            return -1;
        return csum[ii] - csum[ii - period];
    }
    

提交回复
热议问题