aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPeter Zijlstra <a.p.zijlstra@chello.nl>2009-09-04 11:26:26 -0400
committerIngo Molnar <mingo@elte.hu>2009-09-04 11:38:15 -0400
commit8a02631a470d6f2ccec7bcf79c1058b0d4240bce (patch)
treee9dc528dff1db2d7165f315f9c05877265561444
parent63d40deb2e7c64ed55943d49f078e09fc4b64b54 (diff)
perf stat: More advanced variance computation
Use the more advanced single pass variance algorithm outlined on the wikipedia page. This is numerically more stable for larger sample sets. Signed-off-by: Peter Zijlstra <a.p.zijlstra@chello.nl> LKML-Reference: <new-submission> Signed-off-by: Ingo Molnar <mingo@elte.hu>
-rw-r--r--tools/perf/builtin-stat.c24
1 files changed, 12 insertions, 12 deletions
diff --git a/tools/perf/builtin-stat.c b/tools/perf/builtin-stat.c
index e9424fa72420..32b5c003f7fc 100644
--- a/tools/perf/builtin-stat.c
+++ b/tools/perf/builtin-stat.c
@@ -79,29 +79,30 @@ static int event_scaled[MAX_COUNTERS];
79 79
80struct stats 80struct stats
81{ 81{
82 double sum; 82 double n, mean, M2;
83 double sum_sq;
84}; 83};
85 84
86static void update_stats(struct stats *stats, u64 val) 85static void update_stats(struct stats *stats, u64 val)
87{ 86{
88 double sq = val; 87 double delta;
89 88
90 stats->sum += val; 89 stats->n++;
91 stats->sum_sq += sq * sq; 90 delta = val - stats->mean;
91 stats->mean += delta / stats->n;
92 stats->M2 += delta*(val - stats->mean);
92} 93}
93 94
94static double avg_stats(struct stats *stats) 95static double avg_stats(struct stats *stats)
95{ 96{
96 return stats->sum / run_count; 97 return stats->mean;
97} 98}
98 99
99/* 100/*
100 * http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance 101 * http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
101 * 102 *
102 * (\Sum n_i^2) - ((\Sum n_i)^2)/n 103 * (\Sum n_i^2) - ((\Sum n_i)^2)/n
103 * s^2 ------------------------------- 104 * s^2 = -------------------------------
104 * n - 1 105 * n - 1
105 * 106 *
106 * http://en.wikipedia.org/wiki/Stddev 107 * http://en.wikipedia.org/wiki/Stddev
107 * 108 *
@@ -114,9 +115,8 @@ static double avg_stats(struct stats *stats)
114 */ 115 */
115static double stddev_stats(struct stats *stats) 116static double stddev_stats(struct stats *stats)
116{ 117{
117 double avg = stats->sum / run_count; 118 double variance = stats->M2 / (stats->n - 1);
118 double variance = (stats->sum_sq - stats->sum*avg)/(run_count - 1); 119 double variance_mean = variance / stats->n;
119 double variance_mean = variance / run_count;
120 120
121 return sqrt(variance_mean); 121 return sqrt(variance_mean);
122} 122}