5 void update_stats(struct stats
*stats
, u64 val
)
10 delta
= val
- stats
->mean
;
11 stats
->mean
+= delta
/ stats
->n
;
12 stats
->M2
+= delta
*(val
- stats
->mean
);
21 double avg_stats(struct stats
*stats
)
27 * http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
29 * (\Sum n_i^2) - ((\Sum n_i)^2)/n
30 * s^2 = -------------------------------
33 * http://en.wikipedia.org/wiki/Stddev
35 * The std dev of the mean is related to the std dev by:
42 double stddev_stats(struct stats
*stats
)
44 double variance
, variance_mean
;
49 variance
= stats
->M2
/ (stats
->n
- 1);
50 variance_mean
= variance
/ stats
->n
;
52 return sqrt(variance_mean
);
55 double rel_stddev_stats(double stddev
, double avg
)
60 pct
= 100.0 * stddev
/avg
;