diff options
Diffstat (limited to 'tools/perf/builtin-stat.c')
-rw-r--r-- | tools/perf/builtin-stat.c | 24 |
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 | ||
80 | struct stats | 80 | struct stats |
81 | { | 81 | { |
82 | double sum; | 82 | double n, mean, M2; |
83 | double sum_sq; | ||
84 | }; | 83 | }; |
85 | 84 | ||
86 | static void update_stats(struct stats *stats, u64 val) | 85 | static 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 | ||
94 | static double avg_stats(struct stats *stats) | 95 | static 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 | */ |
115 | static double stddev_stats(struct stats *stats) | 116 | static 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 | } |