function double GetSequentialAverage( double average, // current average double value, // new value int length // current length of a sequence ){ return average - (average + value)/(length + 1); }
The method calculates a new average in about 0.0000016 sec.
At the same time, when you need to calculate an average for a whole sequence the best one is the simplest one. Below you can find a benchmark of three average functions:
- Linq extension .Average()
- the sequential average
- and the simple one
Items Number | Linq | Sequential | Simple |
---|---|---|---|
10 | 0.0022526 | 0.0009596 | 0.0004526 |
100 | 0.0024495 | 0.0009739 | 0.0004636 |
1000 | 0.0023505 | 0.0010526 | 0.0004658 |
10000 | 0.0026887 | 0.0013248 | 0.0005741 |
100000 | 0.0046928 | 0.0044321 | 0.0013848 |
1000000 | 0.0231775 | 0.0358475 | 0.0078798 |
10000000 | 0.2114584 | 0.3422293 | 0.0735353 |
[visualizer id=”70″]
[visualizer id=”71″]
Thus the sequential average fits best to a special use case of continuous calculations, when you’re able to retain information about the sequence.